测试代码段:
public class MybatisTest {
private InputStream in = null;
private SqlSession session =null;
private IUserDao userDao = null;
@Before
public void init() throws Exception{
//读取配置文件
in = Resources.getResourceAsStream("SqlMapConfig.xml");
//创建一个SqlSessionFactory工厂
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//使用工厂生产一个SQLSession
session = factory.openSession();
//使用SQLSession创建Dao接口的代理对象
userDao = session.getMapper(IUserDao.class);
}
@After
public void destroy() throws Exception{
session.commit();
session.close();
in.close();
}
@Test
public void findAll(){
List<User> users = userDao.findAll();
for (User user : users) {
System.out.println(user);
}
}
@Test
public void add(){
User user = new User();
user.setUsername("lin");
user.setAddress("郑州市高新区科学大道100号");
user.setBirthday(new Date());
userDao.add(user);
}
@Test
public void update(){
User user = new User();
user.setId(50);
user.setSex("男");
userDao.update(user);
}
@Test
public void delete(){
userDao.delete(50);
}
@Test
public void selectById(){
User user = userDao.select(41);
System.out.println(user);
}
@Test
public void selectByName(){
String name = "%" + "王" + "%";
List<User> users = userDao.selectByName(name);
for (User user : users) {
System.out.println(user);
}
}
@Test
public void findTotal(){
int total = userDao.findTotal();
System.out.println(total);
}
}
vo接口
public class User implements Serializable {
private int id;
private String username;
private Date birthday;
private String sex;
private String address;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/schema/mybatis-3-mapper.dtd">
<mapper namespace="com.zzu.domain.dao.IUserDao">
<!--配置查询所有-->
<select id="findAll" resultType="com.zzu.domain.vo.User">
select * from user;
</select>
<insert id="add" parameterType="com.zzu.domain.vo.User">
insert into user(username,address,sex,birthday) values(#{username},#{address},#{sex},#{birthday})
</insert>
<update id="update" parameterType="com.zzu.domain.vo.User">
update user set sex=#{sex} where id = #{id}
<!-- <trim prefix="set" suffixOverrides="," suffix="where id=#{id}">-->
<!-- <if test="username != null">username=#{username},</if>-->
<!-- <if test="address != null">address=#{address},</if>-->
<!-- <if test="sex != null">sex=#{sex},</if>-->
<!-- <if test="birthday != null">birthday=#{birthday},</if>-->
<!-- </trim>-->
</update>
<delete id="delete" parameterType="int">
delete from user where id = #{ui}
</delete>
<select id="select" parameterType="int" resultType="com.zzu.domain.vo.User">
select * from user where id=#{id}
</select>
<select id="selectByName" parameterType="String" resultType="com.zzu.domain.vo.User">
select * from user where username like #{name}
</select>
<select id="findTotal" resultType="int">
select count(id) from user
</select>
</mapper>
接口
public interface IUserDao {
//查找所有数据
List<User> findAll();
//添加数据
void add(User user);
//更新数据
void update(User user);
//根据id删除用户
void delete(int id);
//根据id查询用户信息
User select(int id);
//根据名称模糊查询
List<User> selectByName(String name);
//查询总用户数
int findTotal();
}