美文网首页
Spring Boot + Mybatis 全注解与XML

Spring Boot + Mybatis 全注解与XML

作者: shpunishment | 来源:发表于2018-09-14 15:45 被阅读0次

    IDEA 创建项目


    创建项目.PNG

    全注解方式

    Mybatis maper的全注解形式,不用dao接口,不用mapper的xml文件,都在一个类里面

    /**
     * mybatis的dao接口和mapper配置文件的合体
     */
    @Mapper
    @Repository
    public interface CustomerMapper {
        @Select("SELECT * FROM customers")
        List<Customer> findAll();
    }
    

    application.properties

    server.port=8080
    server.servlet.context-path=/boot
    
    spring.mvc.view.prefix=/WEB-INF/views/
    spring.mvc.view.suffix=.jsp
    
    # mysql
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/shpun?useUnicode=true&characterEncoding=UTF-8
    spring.datasource.username=root
    spring.datasource.password=root
    

    测试通过

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class TestspringbootApplicationTests {
    
        @Autowired
        private CustomerMapper customerMapper;
    
        @Test
        public void testCustomerMapper(){
            System.out.println(customerMapper.findAll());
        }
    
    }
    

    xml方式

    mybatis-config.xml mybatis的配置文件

    <?xml version="1.0" encoding="utf-8" ?>
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    
        <settings>
            <!-- 全局映射器启用缓存 -->
            <setting name="cacheEnabled" value="true"/>
            <!-- 允许JDBC支持生成的键 -->
            <setting name="useGeneratedKeys" value="true"/>
            <!-- 配置默认执行器 Reuse 重用预处理语句 -->
            <setting name="defaultExecutorType" value="REUSE"/>
            <!-- 全局启用延迟加载 -->
            <setting name="lazyLoadingEnabled" value="true"/>
            <!-- 设置超时时间,决定驱动等待一个数据库相应的时间 -->
            <setting name="defaultStatementTimeout" value="2500"/>
        </settings>
    
    </configuration>
    

    CustomerMapper.xml mapper的xml文件

    <?xml version="1.0" encoding="utf-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.shpun.dao.CustomerDao">
        <select id="selectAllCustomers" resultType="customer">
            select * from customers
        </select>
    </mapper>
    

    application.properties

    server.port=8888
    server.servlet.context-path=/yml
    
    spring.mvc.view.prefix=/WEB-INF/views/
    spring.mvc.view.suffix=.jsp
    
    # mysql
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/shpun?useUnicode=true&characterEncoding=UTF-8
    spring.datasource.username=root
    spring.datasource.password=root
    
    # mybatis
    mybatis.type-aliases-package=com.shpun.bean
    mybatis.config-location=classpath:mybatis/mybatis-config.xml
    mybatis.mapper-locations=classpath:mapper/*.xml
    

    dao 接口

    @Mapper
    @Repository
    public interface CustomerDao{
        List<Customer> selectAllCustomers();
    }
    

    测试通过

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class TestspringbootApplicationTests {
    
        @Autowired
        private CustomerDao customerDao;
    
        @Test
        public void testCustomerDao(){
    
            System.out.println(customerDao.selectAllCustomers());
    
        }
    }
    

    ps:

    先前用xml的时候,提示错误找不到dao的bean,在dao上添加

    @Mapper

    如果是多个dao接口的话,每个都加比较麻烦,可以在SpringBootApplication的java类中添加

    @MapperScan("com.shpun.dao")

    参考 如何优雅的使用mybatis

    相关文章

      网友评论

          本文标题:Spring Boot + Mybatis 全注解与XML

          本文链接:https://www.haomeiwen.com/subject/wtvogftx.html