当你使用最基本的mybatis,可以进行如下集成:
1.application.properties文件
mybatis.type-aliases-package=com.flying.bean
mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
2.mybatis-config.xml代码如下:
<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>
</configuration>
3.mapper类(名称与xml文件对应)
3.1xml代码如下:
<?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.flying.mapper.TestMapper" >
<resultMap id="BaseResultMap" type="com.flying.bean.Test" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="code" property="name" jdbcType="VARCHAR" />
<result column="type" property="type" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List">
code, type
</sql>
<select id="getAll" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
FROM config
</select>
<insert id="insert" parameterType="com.flying.bean.Test">
INSERT INTO config (<include refid="Base_Column_List" />) VALUES(#{code},#{type,jdbcType=VARCHAR})
</insert>
<insert id="batchInsert" parameterType="com.flying.bean.Test">
INSERT INTO config (<include refid="Base_Column_List" />)
<foreach collection ="list" item="c" index="index" separator=" union all ">
SELECT #{c.code}, #{c.type,jdbcType=VARCHAR} FROM DUAL
</foreach>
</insert>
<delete id="deleteAll">
DELETE FROM config
</delete>
</mapper>
4.Service层调用
@Autowired
private TextMapper testMapper;