mybatis整合spring boot

  1. 在pom.xml加入如下内容
    1
    2
    3
    4
    5
    <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.0</version>
    </dependency>
  1. mybatis-config.xml
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <?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>
    <typeAliases>
    <typeAlias alias="Integer" type="java.lang.Integer"/>
    <typeAlias alias="Long" type="java.lang.Long"/>
    <typeAlias alias="HashMap" type="java.util.HashMap"/>
    <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap"/>
    <typeAlias alias="ArrayList" type="java.util.ArrayList"/>
    <typeAlias alias="LinkedList" type="java.util.LinkedList"/>
    </typeAliases>
    <typeAliases>
    <package name="cn.tisson.wechat.job.pojo"/>
    </typeAliases>
    <mappers>
    <mapper resource="cn/tisson/wechat/job/mapper/UserMapper.xml"/>
    </mappers>
    </configuration>

或者使用在application.yml中加入

1
2
3
4
5
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
type-aliases-package: cn.tisson.wechat.job.pojo
# mapper-locations这个配置参数仅当mapper xml与mapper class不在同一个目录下时有效。所以一般可以忽略。
# mapper-locations: classpath:cn/tisson/wechat/job/mapper/*.xml

在Mapper类上面使用 @Mapper 注解

1
2
3
4
@Mapper
public interface UserMapper {
long count(User user);
}

Mapper.xml配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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="cn.tisson.wechat.job.mapper.UserMapper">
<resultMap id="BaseResultMap" type="cn.tisson.wechat.job.pojo.User">
<constructor>
<arg column="ID" javaType="java.math.BigDecimal" jdbcType="DECIMAL"/>
<arg column="AGE" javaType="java.math.BigDecimal" jdbcType="DECIMAL"/>
<arg column="CITYID" javaType="java.math.BigDecimal" jdbcType="DECIMAL"/>
</constructor>
</resultMap>

<select id="count" parameterType="cn.tisson.wechat.job.pojo.User" resultType="java.lang.Long">
select count(*) from user where cityid = #{user.cityid}
</select>
</mapper>

  • 本文作者: forever杨
  • 本文链接: https://blog.yl-online.top/posts/ec4f6c67.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。如果文章内容对你有用,请记录到你的笔记中。本博客站点随时会停止服务,请不要收藏、转载!