简介
应用的项目中基本上都少不了连接到对于的数据库;现在主流的用法springboot+mybatis+mysql 的配置内容;给出的springboot 默认帮我们配置了很对的配置内容项;需要我们配置的内容少之又少了;下面就给出最简答配置的实现;基本上增删改查/事物都能实现
项目的目录
代码内容
1.application.properties文件
spring.datasource.type = com.alibaba.druid.pool.DruidDataSource
spring.datasource.name = data
spring.datasource.url = jdbc:mysql://localhost:3306/khy
spring.datasource.username = root
spring.datasource.password = xxxx
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.filters = stat
spring.datasource.maxActive = 20
spring.datasource.initialSize = 1
spring.datasource.maxWait = 60000
spring.datasource.minIdle = 1
spring.datasource.timeBetweenEvictionRunsMillis = 60000
spring.datasource.minEvictableIdleTimeMillis = 300000
spring.datasource.validationQuery = select 'x'
spring.datasource.testWhileIdle = true
spring.datasource.testOnBorrow = false
spring.datasource.testOnReturn = false
spring.datasource.poolPreparedStatements = true
spring.datasource.maxOpenPreparedStatements = 20
默认的mysql配置;springboot启动会自动加载当前数据源配置;
2.pom.xml
4.0.0
com.khy.boot
boot-mysql
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.4.RELEASE
UTF-8
UTF-8
1.8
com.khy.SpringBootMainApplication
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
mysql
mysql-connector-java
runtime
com.alibaba
druid
1.0.29
com.github.pagehelper
pagehelper
5.0.4
org.apache.commons
commons-lang3
3.4
com.alibaba
fastjson
1.2.6
org.springframework.boot
spring-boot-maven-plugin
3. SpringBootMainApplication.java
@SpringBootApplication
public class SpringBootMainApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMainApplication.class);
}
}
4.SessionFactoryConfig.java
@Configuration
@MapperScan("com.khy.mapper")
public class SessionFactoryConfig {
/** * mybatis 配置路径 */
private static String MYBATIS_CONFIG = "mybatis-config.xml";
private String typeAliasPackage = "com.khy.entity";
@Autowired
private DataSource dataSource;
/** *创建sqlSessionFactoryBean 实例 * 并且设置configtion 如驼峰命名.等等 * 设置mapper 映射路径 * 设置datasource数据源 * @return * @throws Exception */
@Bean(name = "sqlSessionFactory")
public SqlSessionFactoryBean createSqlSessionFactoryBean() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
/** 设置mybatis configuration 扫描路径 */
sqlSessionFactoryBean.setConfigLocation(new ClassPathResource(MYBATIS_CONFIG));
/** 设置datasource */
sqlSessionFactoryBean.setDataSource(dataSource);
/** 设置typeAlias 包扫描路径 */
sqlSessionFactoryBean.setTypeAliasesPackage(typeAliasPackage);
return sqlSessionFactoryBean;
}
}
需要注意@MapperScan 扫描的包结构类型;mapper对应的xml文件也需要放到resource 对应的包结构下面,否则操作数据库的时间会报错
默认配置这样配置,事物管理器也是springboot 会默认DataSourceTransactionManagerAutoConfiguration里面帮我们创建,DataSourceTransactionManager,所以代码中我们需要实现事物的地方就用@Transactional 注解打在上面就能实现;
5.UserServiceImpl
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Transactional
public List find() {
return userMapper.find();
}
@Transactional
public int insert(String name) {
User user=new User();
user.setUserName(name);
user.setAge(100);
user.setBirthday(new Date());
user.setScore(1000);
user.setMoney(10000.00);;
int count = userMapper.insertEntity(user);
if(count!=0){
// throw new RuntimeException("自定义异常信息");
}
user.setUserName("事务");
return userMapper.insertEntity(user);
}
}
总结
在service中我们通过@Transactional注解实现事物内容;我们自定义的异常则代码执行完毕之后事物会回滚;整个事物内容无需我们在额外配置任何东西; 当前案例是最简答配置化的;当然也可以在SessionFactoryConfig 里面配置我们自己的事物管理器
只需要实现 TransactionManagementConfigurer 接口
实现下面的方法内容;
@Bean
public PlatformTransactionManager annotationDrivenTransactionManager() {
return new DataSourceTransactionManager(dataSource);
}