<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.gjk.mapper.UserMapper"><selectid="getUser"resultType="com.gjk.bean.User">
select *
from user
</select><selectid="getUserById"parameterType="int"resultType="com.gjk.bean.User">
select *
from user
where id = #{userId}
</select><!-- 可以直接从实体类中获取到值--><insertid="addUser"parameterType="com.gjk.bean.User">
insert into user(name, pwd) value (#{name},#{pwd})
</insert><!-- 参数可以为map这样的话取值的时候需要和map中的key对应--><insertid="addUserMap"parameterType="map">
insert into user(name, pwd) value (#{userName},#{userPwd})
</insert><updateid="updateUser"parameterType="com.gjk.bean.User">
update user
set name =#{name},
pwd =#{pwd}
where id = #{id}
</update><deleteid="delUser"parameterType="int">
delete
from user
where id = #{userId}
</delete></mapper>
测试代理
@org.junit.Testpublicvoidtest(){//找到sqlSession SqlSession sqlSession =MybatisUtil.getSqlSession();//获取对应MapperUserMapper mapper = sqlSession.getMapper(UserMapper.class);List<User> user = mapper.getUser();for(User u : user){System.out.println(u.getName());}//关闭当前sqlSession
sqlSession.close();}