spring + mybatis整合(java配置方式)

一、导入spring 和 mybatis 的依赖。

1、spring核心容器的五个依赖、spring单元测试依赖(此依赖和spring核心版本最好一致。)

        
            1.8
            5.0.4.RELEASE
        
        
            org.springframework
            spring-expression
            ${spring.version}
        
        
            org.springframework
            spring-core
            ${spring.version}
        
        
            org.springframework
            spring-beans
            ${spring.version}
        
        
            org.springframework
            spring-context-support
            ${spring.version}
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-test
            5.0.4.RELEASE
        
复制代码

2、 日志的依赖加入slf4j日志门面,和 logback实现。

        
            org.slf4j
            slf4j-api
            1.7.25
        
        
            ch.qos.logback
            logback-classic
            1.2.3
        
复制代码

3、加入mybatis 和Spring整合相关的依赖

        
            mysql
            mysql-connector-java
            5.1.44
            runtime
        
        
            com.alibaba
            druid
            1.1.6
        
        
            org.mybatis
            mybatis-spring
            1.3.1
        
        
            org.mybatis
            mybatis
            3.4.5
        
        
        
            org.springframework
            spring-tx
            5.0.4.RELEASE
        
        
        
            org.springframework
            spring-jdbc
            4.3.13.RELEASE
        
复制代码

二、Java配置方式,编写配置文件

@Configuration
@MapperScan(basePackages = "com.demo.sm.mapper")
@ComponentScan("com.demo.sm")
public class SmConfig {
    // 这里面要手动配置两个bean dataSource和SqlSessionFactory
    @Bean
    public DruidDataSource dataSource() {
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setUrl("jdbc:mysql://localhost:3306/test-20180410?characterEncoding=utf-8&useSSL=true");
        ds.setUsername("root");
        ds.setPassword("root");
        ds.setInitialSize(5);
        return ds;
    }
    
    @Bean
    public SqlSessionFactory sqlSessionFactory () throws Exception{
        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        // 设置xml的映射文件位置
        sessionFactory.setMapperLocations(resourcePatternResolver.getResources("classpath:mappings/*.xml"));
        // 设置别名的包,可以在xml中直接使用user,
        sessionFactory.setTypeAliasesPackage("com.demo.sm.entity");
        return sessionFactory.getObject();
    }
}
复制代码

三、在mapper 中编写UserMapper的接口,和在resources的mappings目录中编写UserMapper.xml。

1、UserMapper.xml文件

"1.0" encoding="UTF-8"?>
"-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

"com.demo.sm.UserMapper">
    type="user" id="User">
        "id" column="id" />
        "name" column="name" />
        "age" column="age" />
    

    

    "insertUser">
        INSERT INTO user (id,name,age) VALUES (#{id},#{name},#{age})
    

    "updateUser" parameterType="user">
        UPDATE user SET name=#{name},age=#{name} WHERE id=#{id}
    

    "deleteUser" parameterType="int">
        DELETE FROM user WHERE id=#{id}
    


复制代码

2、UserMapper接口。

@Mapper
public interface UserMapper {
    User getUser(String name);
    void insertUser(User user);
    void deleteUser(int id);
    void updateUser(User user);
}
复制代码

四、在数据库中就建一张表,只包含三个字段。测试成功。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SmConfig.class)
public class Test {
    @Autowired
    private UserService userService;
    
    @org.junit.Test
    public void test2() {
        User java = userService.getUser("java");
        System.out.println(java);
        Assert.assertNotNull(java);
    }
}
复制代码

你可能感兴趣的:(spring + mybatis整合(java配置方式))