spring-boot集成mybatis

2018-06-18 03:44:15来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

前提条件:已经创建一个spring-boot项目

集成步骤:

  1. 添加依赖
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
  2. application.properties中增加如下配置
    spring.datasource.driverClassName = com.mysql.jdbc.Driver
    spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
    spring.datasource.username = root
    spring.datasource.password = 1234   
    
    mybatis.mapperLocations = classpath:mapper/*Dao.xml
    mybatis.configLocation = classpath:mybatis-config.xml
  3. resources下增加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="mapUnderscoreToCamelCase" value="true" />
        </settings>
    </configuration>
  4. 创建与表字段进行映射的实体类User
    package com.ghaien.entity;
    
    import lombok.Getter;
    import lombok.Setter;
    import java.io.Serializable;
    import java.util.Date;
    
    @Getter
    @Setter
    public class User implements Serializable {
        private static final long serialVersionUID = -5343460982269922412L;
    
        private Long id;
    
        private String userName;
    
        private String password;
    
        private Date createTime;
    
        private Date updateTime;
    
        private Integer enable;
    }

    上面代码中的Getter和Setter注解可以让编译器帮你自动添加所有属性的get和set方法。也可以通过快捷键直接生成,若要使用以上方法需要引入如下依赖

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.18</version>
        <scope>provided</scope>
    </dependency>

    还需要引入插件

    <plugin>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok-maven-plugin</artifactId>
        <version>1.16.18.0</version>
    </plugin>
  5. 对应配置文件中路径创建UserDao.xml,并编写相关sql
    <?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.ghaien.dao.UserDao">
        <sql id="allColumn">
            id, user_name, password, create_time, update_time, enable
        </sql>
        <select id="queryAll" resultType="com.ghaien.entity.User">
            select <include refid="allColumn"/>
            from user
            where enable = 1
        </select>
    
        <select id="queryByUserName" resultType="com.ghaien.entity.User">
            select <include refid="allColumn"/>
            from user
            where enable = 1
            and user_name = #{userName}
        </select>
    </mapper>

    因为有了如上mybatis-config.xml的配置,所以如user_name会自动映射到User的userName属性中,通过在mapper下添加resultMap也能有相同效果

  6. 创建UserDao接口代码如下所示
    package com.ghaien.dao;
    
    import com.ghaien.entity.User;
    import org.apache.ibatis.annotations.Param;
    import java.util.List;
    
    public interface UserDao {
    
        List<User> queryAll();
    
        User queryByUserName(@Param(value = "userName") String userName);
    }

    若没有在Application类上增加如下注解,则需在UserDao上增加@Mapper

    @MapperScan("com.ghaien.dao")
  7. 创建测试类检查是否成功集成mybatis,代码如下所示
    package com.ghaien.dao;
    
    import com.ghaien.entity.User;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    import java.util.List;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class UserDaoTest {
    
        private Logger log = LoggerFactory.getLogger(this.getClass());
    
        @Autowired
        private UserDao userDao;
    
        @Test
        public void testQueryAll() {
            List<User> userList = userDao.queryAll();
            log.info("userList.size() = " + userList.size());
            for (User user : userList) {
                log.info("user.userName = " + user.getUserName());
            }
        }
    }

    运行testQueryAll查看输出,也可以直接编写service和controller后在网页上查看结果,具体代码我就不贴出来了

完整代码地址

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:java基础面试题:java中实现多态的机制是什么?

下一篇:镜像队列