UserMapper(Interface)
package com.iot.mybatis.mapper;
import com.iot.mybatis.po.User;
//接口中的方法名,输入参数的类型,返回值类型和mapper.xml中的statement的id一致,
public interface UserMapper {
public User findUserById(int id) throws Exception;
}
UserMapper.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.xml中,命名空间的地址就是mapper接口的地址-->
<mapper namespace="com.iot.mybatis.mapper.UserMapper">
<select id="findUserById" parameterType="int" resultType="com.iot.mybatis.po.User">
select * from user where id=#{value}
</select>
<select id="findUserByName" parameterType="String" resultType="com.iot.mybatis.po.User">
select * from user where username like '%${value}%'
</select>
<insert id="insertUser" parameterType="com.iot.mybatis.po.User">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into user (username,birthday,sex,address) value (#{username},#{birthday},#{sex},#{address})
</insert>
<delete id="deleteUser" parameterType="java.lang.Integer">
delete from user where id=#{value}
</delete>
<update id="updateUser" parameterType="com.iot.mybatis.po.User">
update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}
</update>
</mapper>
sqlMapConfig.xml
<?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>
<!--数据库环境-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydb1?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--用于配置映射器-->
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>