接着上文《Mybatis在Spring中的使用(二)》。本文介绍了mybaits基本的增查改删操作以及参数的传递
//IUserDao.java
package mybatis.dao;
import mybatis.annotation.Select;
import mybatis.domain.User;
import java.util.List;
public interface IUserDao {
/**
* 查询所有操作
* */
//@Select("select * from user")
List findAll();
/**
* 保存用户
* @param user
*/
void saveUser(User user);
/**
* 更新用户
* @param user
*/
void updateUser(User user);
/**
* 根据ID删除user
* @param userId
*/
void deleteUser(Integer userId);
/**
* 根据id查询用户信息
* @param userId
* @return
*/
User findById(Integer userId);
/**
* 根据名称模糊查询用户名称
* @param userName
* @return
*/
List findByName(String userName);
/**
* 查询总用户数
* @return
*/
int findTotal();
}
IUserDao.xml的配置文件如下
insert into user(username,address,sex,birthday)values(#{username},#{address},#{sex},#{birthday});
update user set username=#{username},address=#{address},sex=#{sex},birthday=#{birthday} where id = #{id};
delete from user where id = #{id}
测试代码如下
import mybatis.dao.IUserDao;
import mybatis.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
public class MybatisTest {
private InputStream in;
private SqlSession sqlSession;
private IUserDao userDao;
@Before//用于在测试方法执行之前执行
public void init() throws IOException {
//1.读取配置文件
in = Resources.getResourceAsStream("SqlMapConfig.xml");
//2.创建SqlSessionFactory工厂
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
//3.使用工厂生产一个SqlSession对象
sqlSession = factory.openSession();
//4.使用SqlSession创建Dao接口的代理对象
userDao = sqlSession.getMapper(IUserDao.class);
}
@After//用于在测试方法之前执行
public void destroy() throws IOException{
sqlSession.commit();
sqlSession.close();
in.close();
}
@Test
public void testFindAll() throws IOException {
List users = userDao.findAll();
for(User user : users){
System.out.println(user);
}
}
/**
* 测试保存操作
*/
@Test
public void testSave() throws IOException {
User user = new User();
user.setUsername("mybatis saveuser");
user.setAddress("beijing");
user.setSex("男");
user.setBirthday(new Date());
userDao.saveUser(user);
}
/**
* 测试更新操作
*/
@Test
public void testUpdate(){
User user = new User();
user.setId(49);
user.setUsername("mybatis newuser");
user.setAddress("beijing");
user.setSex("女");
user.setBirthday(new Date());
userDao.updateUser(user);
}
/**
* 执行删除操作
*/
@Test
public void testDelete(){
userDao.deleteUser(49);
}
/**
* 测试查询一个用户
*/
@Test
public void testFindOne(){
User user = userDao.findById(48);
System.out.println(user);
}
/**
* 模糊查询
*/
@Test
public void testFindByName(){
List users = userDao.findByName("%王%");
for(User user : users){
System.out.println(user);
}
}
/**
* 测试查询总记录条数
*/
@Test
public void testFindTotal(){
int count = userDao.findTotal();
System.out.println(count);
}
}
对于模糊查询
也可以写成
<select id="findByName" parameterType="string" resultType="mybatis.domain.User">
select * from user where username like '%${value}%';
select>
${}中必须要写成value
因为在源码绑定sql时,是直接写死了value这个单词
写成下面
<select id="findByName" parameterType="string" resultType="mybatis.domain.User">
select * from user where username like #{userName}
select>
在底层是用PreparedStatement的参数占位符,
而写成
<select id="findByName" parameterType="string" resultType="mybatis.domain.User">
select * from user where username like '%${value}%';
select>
在底层是用Statement对象的字符串拼接SQL,所以采用#{userName}写法更好
select last_insert_id();
insert into user(username,address,sex,birthday)values(#{username},#{address},#{sex},#{birthday});
@Test
public void testSave() throws IOException {
User user = new User();
user.setUsername("mybatis saveuser insertid");
user.setAddress("beijing");
user.setSex("男");
user.setBirthday(new Date());
System.out.println("before : " + user);
userDao.saveUser(user);
System.out.println("after : " + user);
}
//IUserDao.java
....
/**
* 根据queryVo中的条件查询用户
* @param vo
* @return
*/
List findUserByVo(QueryVo vo);
package mybatis.domain;
public class QueryVo {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
<select id="findUserByVo" parameterType="mybatis.domain.QueryVo" resultType="mybatis.domain.User">
select * from user where username like #{user.username}
select>
@Test
public void testFindByVo(){
QueryVo vo = new QueryVo();
User user = new User();
user.setUsername("%王%");
vo.setUser(user);
List users = userDao.findUserByVo(vo);
for(User u : users){
System.out.println(u);
}
}
<resultMap id="userMap" type="mybatis.domain.User">
<id property="userId" column="id">id>
<result property="userName" column="username">result>
resultMap>
<select id="findAll" resultMap="userMap">
select * from user
select>