<select id="selectUserById" resultType="com.PAG.pojo.User">
select * from user where id = #{id}
</select>
测试类
@Test
public void selectUserById(){
SqlSession session = MybatisUtils.getSession();
userMapper mapper = session.getMapper(userMapper.class);
User user = mapper.selectUserById(1);
System.out.println(user);
session.close();
}
通过名字和密码查询用户(Map)
<select id="selectUserByMap" parameterType="map" resultType="com.PAG.pojo.User">
select * from user where name = #{name} and pwd = #{pwd}
</select>
测试类
@Test
public void selectUserByMap(){
SqlSession session = MybatisUtils.getSession();
userMapper mapper = session.getMapper(userMapper.class);
Map<String, Object> map = new HashMap<String,Object>();
map.put("name","pag");
map.put("pwd","123456");
User user = mapper.selectUserByMap(map);
System.out.println(user);
session.close();
}
insert
<insert id="addUser" parameterType="com.PAG.pojo.User">
insert into user(id,name,pwd) values (#{id},#{name},#{pwd})
</insert>
测试类
@Test
public void addUser(){
SqlSession session = MybatisUtils.getSession();
userMapper mapper = session.getMapper(userMapper.class);
User user = new User(3,"p","1212");
int i = mapper.addUser(user);
System.out.println(i);
session.commit();
session.close();
}
update
<update id="updateUser" parameterType="com.PAG.pojo.User">
update user set name = #{name},pwd = #{pwd} where id = #{id}
</update>
测试类
@Test
public void updateUser(){
SqlSession session = MybatisUtils.getSession();
userMapper mapper = session.getMapper(userMapper.class);
User user = mapper.selectUserById(3);
user.setPwd("2333");
int i = mapper.updateUser(user);
System.out.println(i);
session.commit();
session.close();
}
delete
<delete id="deleteUser" parameterType="com.PAG.pojo.User">
delete from user where id = #{id}
</delete>
测试类
@Test
public void deleteUser(){
SqlSession session = MybatisUtils.getSession();
userMapper mapper = session.getMapper(userMapper.class);
int i = mapper.deleteUser(3);
System.out.println(i);
session.commit();
session.close();
}
}