不再通过编写UserInfoMapper.xml映射文件的方式,而是直接定义接口UserInfoMapper.java,并且在接口中使用注解来实现增删改查。
使用接口注解的优点:简单方便,能够快速编写映射语句。
使用接口注解的缺点:
1. 只适用于比较简单的数据处理,不需要写配置文件,太复杂的话就不行了。
2. 不能使用动态SQL。
一、修改全局配置文件mybatisconfig.xml的映射器部分:
<!-- 映射器 -->
<mappers>
<mapper class="com.sun.mapper.UserInfoMapper"></mapper>
</mappers>
二、创建接口UserInfoMapper.java
package com.sun.mapper;
import java.util.HashMap;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.sun.po.UserInfo;
public interface UserInfoMapper {
/**
* 查询所有用户信息
* @return
*/
@Select(value="select * from user_info2")
public List<HashMap> getUserList();
/**
* 根据提交查询用户信息
* @return
*/
@Select(value="select * from user_info2 where user_id=#{userId}")
@Results({@Result(property="userId",column="user_id"),
@Result(property="userName",column="user_name"),
@Result(property="userSex",column="user_sex")})
public List<UserInfo> selectUserList(UserInfo user);
@Select(value="select * from user_info2 where user_id=#{userId} and user_name=#{userName}")
@Results({@Result(property="userId",column="user_id"),
@Result(property="userName",column="user_name"),
@Result(property="userSex",column="user_sex")})
public UserInfo selectUser(@Param("userId") Integer userId, @Param("userName") String userName);
@Select(value="select * from user_info2 where user_sex=#{userSex}")
@Results({@Result(property="userId",column="user_id"),
@Result(property="userName",column="user_name"),
@Result(property="userSex",column="user_sex")})
public List<UserInfo> selectUsers(String userSex);
/**
* 添加用户信息
* @param user
* @return
*/
@Insert(value="Insert into user_info2(user_name,user_sex) values(#{userName},#{userSex})")
public int addUser(UserInfo user);
/**
* 修改用户信息
* @param user
* @return
*/
@Update(value="update user_info2 set user_name=#{userName},user_sex=#{userSex} where user_id=#{userId}")
public int updateUser(UserInfo user);
/**
* 根据编号删除用户信息
* @param userId
* @return
*/
@Delete(value="delete from user_info2 where user_id=#{userId}")
public int deleteUser(Integer userId);
}
三、创建测试程序UserTest.java
package com.sun.test;
import java.util.HashMap;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Before;
import org.junit.Test;
import com.sun.mapper.UserInfoMapper;
import com.sun.po.UserInfo;
import com.sun.utils.DBFactory;
public class UserTest {
private SqlSession session;
UserInfoMapper mapper = null;
@Before
public void init() {
session = DBFactory.getSqlsession();
mapper = session.getMapper(UserInfoMapper.class);
}
@Test
public void select1() {
List<HashMap> list = mapper.getUserList();
System.out.println(list.size());
for(HashMap map:list) {
System.out.println("编号:"+map.get("user_id")+" 姓名:"+map.get("user_name")+" 性别:"+map.get("user_sex"));
}
}
@Test
public void select2() {
UserInfo user = new UserInfo();
user.setUserId(3);
List<UserInfo> list = mapper.selectUserList(user);
for(UserInfo userInfo: list) {
System.out.println(userInfo);
}
}
@Test
public void select3() {
UserInfo user = mapper.selectUser(2, "李逵");
System.out.println(user);
}
@Test
public void select4() {
List<UserInfo> list = mapper.selectUsers("男");
for(UserInfo user: list) {
System.out.println(user);
}
}
@Test
public void add() {
UserInfo user = new UserInfo();
user.setUserName("扈三娘");
user.setUserSex("女");
int count = mapper.addUser(user);
if(count>0) {
System.out.println("添加成功!");
}else {
System.out.println("添加失败!");
}
session.commit();
session.close();
}
@Test
public void update() {
UserInfo user = new UserInfo();
user.setUserId(5);
user.setUserName("吴用");
user.setUserSex("男");
int count = mapper.updateUser(user);
if(count>0) {
System.out.println("修改成功!");
}else {
System.out.println("修改失败!");
}
session.commit();
session.close();
}
@Test
public void delete() {
int count = mapper.deleteUser(5);
if(count>0) {
System.out.println("删除成功!");
}else {
System.out.println("删除失败!");
}
session.commit();
session.close();
}
}
四、采用单元测试框架JUnit依次测试
数据库初始内容:
运行select1,成功查询出用户数据总条数为4,以及所有用户信息内容:
运行select2,成功查询出编号为3的用户信息:
运行select3,成功查询出编号为2、姓名为李逵的用户信息:
运行select4,成功查询出所有性别为男的用户信息:
运行add,成功添加姓名为扈三娘、性别为女的用户信息:
运行update,成功修改编号为5的用户姓名为吴用、性别为男:
运行delete,成功删除编号为5的用户信息: