# Spring 整合 MyBatis
将 Mybatis 原来的配置文件:Mybatis-config.xml 替换成了 Spring 来整合它
1. 创建目录
2. 创建对应的类
代码
# config
| @SuppressWarnings("all") |
| public class JdbcConfig { |
| |
| @Value("${driver}") |
| private String driver; |
| @Value("${jdbcUrl}") |
| private String url; |
| @Value("${a.username}") |
| private String ausername; |
| @Value("${password}") |
| private String password; |
| |
| @Bean |
| public DataSource getDataSource() { |
| DruidDataSource d = new DruidDataSource(); |
| |
| d.setDriverClassName(driver); |
| d.setUrl(url); |
| d.setUsername(ausername); |
| d.setPassword(password); |
| |
| return d; |
| } |
| } |
| @SuppressWarnings("all") |
| public class MybatisConfig { |
| |
| @Bean |
| public SqlSessionFactoryBean getSqlSessionFactory(DataSource d){ |
| |
| SqlSessionFactoryBean sqlsession = new SqlSessionFactoryBean(); |
| |
| sqlsession.setTypeAliasesPackage("com.dkx.spring.domain"); |
| |
| sqlsession.setDataSource(d); |
| return sqlsession; |
| } |
| |
| @Bean |
| public MapperScannerConfigurer mappersacnnerconfigurer(){ |
| |
| MapperScannerConfigurer mapper = new MapperScannerConfigurer(); |
| |
| mapper.setBasePackage("com.dkx.spring.dao"); |
| return mapper; |
| } |
| } |
| @SuppressWarnings("all") |
| |
| @Configuration |
| |
| @ComponentScan("com.dkx.spring") |
| |
| @PropertySource("classpath:druid.properties") |
| |
| @Import({JdbcConfig.class,MybatisConfig.class}) |
| public class SpringConfig { |
| } |
# dao
| @SuppressWarnings("all") |
| public interface UserMapper { |
| @Insert("insert into spring_mybatis (id,name,money) values (#{id},#{name},#{money})") |
| int save(SpringMybatis s); |
| @Delete("delete spring_mybatis where id = #{id}") |
| int delete(); |
| @Update("update from spring_mybatis set name = #{name} where id = #{id}") |
| int update(); |
| @Select("select * from user") |
| List<SpringMybatis> getList(); |
| @Select("select * from user where id = #{id}") |
| SpringMybatis getUserById(Integer id); |
| } |
# domian
| @SuppressWarnings("all") |
| public class SpringMybatis { |
| private Integer id; |
| private String name; |
| private Integer money; |
| public SpringMybatis() { |
| } |
| |
| public SpringMybatis(Integer id, String name, Integer money) { |
| this.id = id; |
| this.name = name; |
| this.money = money; |
| } |
| |
| public Integer getId() { |
| return id; |
| } |
| |
| public void setId(Integer id) { |
| this.id = id; |
| } |
| |
| public String getName() { |
| return name; |
| } |
| |
| public void setName(String name) { |
| this.name = name; |
| } |
| |
| public Integer getMoney() { |
| return money; |
| } |
| |
| public void setMoney(Integer money) { |
| this.money = money; |
| } |
| |
| @Override |
| public String toString() { |
| return "SpringMybatis{" + |
| "id=" + id + |
| ", name='" + name + '\'' + |
| ", money=" + money + |
| '}'; |
| } |
| } |
# service
| @SuppressWarnings("all") |
| public interface UserService { |
| void save(SpringMybatis s); |
| void delete(); |
| void update(); |
| List<SpringMybatis> getList(); |
| SpringMybatis getUserById(Integer id); |
| } |
# impl
| @SuppressWarnings("all") |
| |
| @Service |
| public class UserServiceImpl implements UserService { |
| |
| @Autowired |
| private UserMapper userMapper; |
| |
| public void save(SpringMybatis s){ |
| userMapper.save(s); |
| } |
| public void delete(){ |
| userMapper.delete(); |
| } |
| public void update(){ |
| userMapper.update(); |
| } |
| public List<SpringMybatis> getList(){ |
| return userMapper.getList(); |
| } |
| public SpringMybatis getUserById(Integer id){ |
| return userMapper.getUserById(id); |
| } |
| } |
# resources
| driver=com.mysql.cj.jdbc.Driver |
| jdbcUrl=jdbc:mysql://localhost:3306/spring_mybatis?characterEncoding=utf-8&serverTimezone=UTC |
| a.username=root |
| password=dkx |
| public void test2(){ |
| ApplicationContext c = new AnnotationConfigApplicationContext(SpringConfig.class); |
| UserService service = c.getBean(UserService.class); |
| List<SpringMybatis> list = service.getList(); |
| for(SpringMybatis i:list){ |
| System.out.println(i); |
| } |
| } |