1. Framework Maven deployment and installation
After downloading the framework source code, execute mvn clean install in the project root path to install it into the local maven library. If you need to share and use Nexus private server, add distributionManagement configuration in the root path pom.xml file, specify the Nexus warehouse distribution address, and use mvn clean deploy to install to the remote maven warehouse, as
<distributionManagement> <repository> <id>nexus-releases</id> <url> http://ip:port/repository/maven-releases/ </url> </repository> <snapshotRepository> <id>nexus-snapshots</id> <url> http://ip:port/repository/maven-snapshots/ </url> </snapshotRepository> </distributionManagement>
specified above The repository needs to have corresponding account configuration in all maven configuration files settings.xml (id needs to correspond one to one), such as
<servers> <server> <id>nexus-snapshots</id> <username>admin</username> <password>xxx</password> </server> <server> <id>nexus-releases</id> <username>admin</username> <password>xxx</password> </server> </servers>
2. pom.xml configuration
There are three ways to introduce this database framework into the project:
Directly introduce cn.jboost.springboot:tkmapper-spring-boot-starter (without connection pool)
Directly introduce cn.jboost.springboot:druid-spring-boot-starter (druid connection pool support)
#第一种方式 <dependency> <groupId>cn.jboost.springboot</groupId> <artifactId>tkmapper-spring-boot-starter</artifactId> <version>1.2-SNAPSHOT</version> </dependency> #第二种方式 <dependency> <groupId>cn.jboost.springboot</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2-SNAPSHOT</version> </dependency> #第三种方式 <parent> <groupId>cn.jboost.springboot</groupId> <artifactId>spring-boot-parent</artifactId> <version>1.2-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent>
3. Configure the data source
spring: datasource: druid: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf-8 username: root password: # 自定义配置 initialSize: 2 # 初始化大小 minIdle: 1 # 最小连接 maxActive: 5 # 最大连接 druidServletSettings: allow: 127.0.0.1 deny: loginUsername: admin loginPassword: Passw0rd resetEnable: true druidFilterSettings: exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*' maxWait: 60000 # 配置获取连接等待超时的时间 timeBetweenEvictionRunsMillis: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 minEvictableIdleTimeMillis: 300000 # 配置一个连接在池中最小生存的时间,单位是毫秒 validationQuery: SELECT 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true # 打开PSCache,并且指定每个连接上PSCache的大小 maxPoolPreparedStatementPerConnectionSize: 20 filters: stat #,wall(添加wall代码里不能直接拼接sql,druid有sql注入校验) # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 useGlobalDataSourceStat: true # 合并多个DruidDataSource的监控数据
spring: datasource: url: jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf-8 username: root password: driver-class-name: com.mysql.jdbc.Driver
4. Define the corresponding domain and mapper , service, controller objects at each layer
@Table(name = "user") @Getter @Setter @ToString public class User extends AutoIncrementKeyBaseDomain<Integer> { private String name; @ColumnType(jdbcType = JdbcType.CHAR) private Gender gender; private List<String> favor; private Map<String, String> address; public enum Gender{ M, F } }
@Repository public interface UserMapper extends BaseMapper<User> { }
@Service public class UserService extends BaseService<Integer, User> { @Transactional public void createWithTransaction(User user){ create(user); //用于测试事务 throw new RuntimeException("抛出异常,让前面的数据库操作回滚"); } }
@RestController @RequestMapping("/user") public class UserController extends BaseController<Integer, User> { }
5. Test and run
The above is the detailed content of How to implement single table operation by integrating Mapper in springboot. For more information, please follow other related articles on the PHP Chinese website!