1. 首先要有个Mapper(Dao)类
2. 在resource下新建一个mapper文件夹,创建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.example.d002.dao.UserDao">
<select id="gerUserById" resultType="User">
select * from user where id=#{id}
</select>
<select id="getUserByAge" resultType="User">
select * from user where age=#{age}
</select>
<select id="getUserByName" resultType="User">
select * from user where username like concat('%',#{username},'%')
</select>
<insert id="insertUser" parameterType="User">
<selectKey keyProperty="id" order="AFTER" resultType="int">
select LAST_INSERT_ID()
</selectKey>
insert into user (username,password,age)
values (#{userName,jdbcType=VARCHAR},#{password,jdbcType=VARCHAR},#{age,jdbcType=INTEGER})
</insert>
</mapper>
3. sql文件中的变量通过@Param传参传进去的
4. 在 application.properties文件中配置xml文件和mapper文件的对应关系