- 在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>
- mybatis-config.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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="top.ylonline.wechat.job.pojo"/>
</typeAliases>
<mappers>
<mapper resource="top/ylonline/wechat/job/mapper/UserMapper.xml"/>
</mappers>
</configuration>
或者在application.yml
中加入1
2
3
4
5mybatis:
config-location: classpath:mybatis/mybatis-config.xml
type-aliases-package: top.ylonline.wechat.job.pojo
# mapper-locations 这个配置参数仅当mapper xml与mapper class不在同一个目录下时有效。所以一般可以忽略。
# mapper-locations: classpath:top/ylonline/wechat/job/mapper/*.xml
在Mapper类上面使用 @Mapper
注解1
2
3
4
public interface UserMapper {
long count(User user);
}
Mapper.xml配置1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<mapper namespace="top.ylonline.wechat.job.mapper.UserMapper">
<resultMap id="BaseResultMap" type="top.ylonline.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="top.ylonline.wechat.job.pojo.User" resultType="java.lang.Long">
select count(*) from user where cityid = #{user.cityid}
</select>
</mapper>