目录
2.1 -----SpringConfiguration配置类
2.2 -----MyBatisConfiguration配置类
2.3 ----- properties文件【该文件要放在resources包下】
Spring整合MyBatis
1. 图集导航
2.配置资源
2.1 -----SpringConfiguration配置类
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
@Configuration //配置类
@PropertySource("classpath:db2.properties") //加载属性文件peoperties
@ComponentScan(basePackages = "com.czxy.demo17_accountSW") //扫描所需要的包
@EnableTransactionManagement //开启事务
public class SpringConfiguration {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url ;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driver);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
return druidDataSource;
}
//事务管理器【管理事务,事务出现异常会进行回滚事务】
@Bean
public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
2.2 -----MyBatisConfiguration配置类
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration //配置类
public class MyBatisConfiguration {
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
//1 创建工厂
// 1.通过工厂bean创建对象,最后需要调用 getObject()获得具体的对象
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
//2 设置数据-- SqlMapConfig.xml 配置信息
// 1.1 设置数据源
factoryBean.setDataSource(dataSource);
// 1.2 设置别名包扫描
factoryBean.setTypeAliasesPackage("要加载的daomain包的全路径");
// 1.3 全局配置:驼峰映射
org.apache.ibatis.session.Configuration config = new org.apache.ibatis.session.Configuration();
config.setMapUnderscoreToCamelCase(true);
factoryBean.setConfiguration(config);
// 2 插件配置
// 2.1 分页插件
PageHelper pageHelper = new PageHelper();
Properties pageProps = new Properties();
pageProps.setProperty("dialect", "mysql");
pageProps.setProperty("rowBoundsWithCount", "true");
pageHelper.setProperties(pageProps);
factoryBean.setPlugins(new Interceptor[] { pageHelper });
// 返回SqlSessionFactory
return factoryBean.getObject();
}
/**
* 扫描Dao的包,查找各种XxxMapper接口,创建好UserMapper等对象存入到IOC的容器中
* @return
*/
@Bean
public MapperScannerConfigurer mapperScanner() {
MapperScannerConfigurer configurer = new MapperScannerConfigurer();
configurer.setBasePackage("要加载的mapper包的全路径");
return configurer;
}
}
2.3 ----- properties文件【该文件要放在resources包下】
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/连接数据库,要访问的数据库的库名
jdbc.username=数据库用户名【一般都是root】
jdbc.password=数据库密码【一般都是1234】
3. 代码数据【可参考】
3.1 UserService接口
public interface UserService {
//查询所有
List<User> selectAll();
}
UserService实现类
-
@Service(value = "userService1") public class UserServiceImpl implements UserService { //依赖注入mapper @Resource private UserMapper userMapper; //查询所有 @Override public List<User> selectAll() { return userMapper.selectAll(); } }
3.2 daomain【JavaBean】
-
User类
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name = "user")
public class User {
@Id
private Integer id ;
private String name ;
private Integer age ;
private Double num ;
//省略构造和set/get方法
}
3.3 Mapper接口
-
UserMapper接口
public interface UserMapper extends Mapper<User> {
}
3.4 配置类
----【见2.配置资源,复制即可---根据需要修改特定位置信息即可】
-
图集导航
-
SpringConfiguration类
-
MyBatisConfiguration类
-
3.5 测试类
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {SpringConfiguration.class, MyBatisConfiguration.class})
public class TestSM {
@Resource(name = "userService1")
private UserService userServiceImpl;
@Test
public void testDemo16(){
List<User> list= userServiceImpl.selectAll();
list.forEach(System.out::println);
}
}
3.6 测试结果
4.注意事项
-
1.MyBatisConfiguration配置类相对应部分要进行更该 ===》 daomain路径 --> mapper路径
-
2.相关配置所需包要注意不要导包导错了
-
3.JavaBean 要使用注解@Table、@Id、@Column来对应数据库信息
-
4.properties文件中要记得更改自己所需 的数据库库名