在这里 首先要搭建好Mybatis的配置环境
增删改一定要记得提交事务,或者openSession时传递true,自动提交
SqlSession sqlSession = sqlSessionFactory.openSession(true);
一、增加
UerDao接口中:
public void addUser(User user);
映射配置文件UserDao.xml中:
<insert id="addUser" parameterType="eud.rk.domain.User">
insert into user (username,address,sex,birthday) values(#{username},#{address},#{sex},#{birthday});
</insert>
测试类:
@Test
public void testSave() throws IOException {
User user=new User();
user.setUsername("张三");
user.setAddress("汉中市西乡县");
user.setSex("男");
user.setBirthday(new Date());
//1、读取配置文件,生成字节输入流
InputStream in= Resources.getResourceAsStream("rk.xml");
//2、获取sqlSessionfactory
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
//3、获取sqlsession对象
SqlSession sqlSession = factory.openSession();
//4、获取dao的代理对象
UserDao userDao = sqlSession.getMapper(UserDao.class);
//5、执行添加方法
userDao.addUser(user);
//提交事务
sqlSession.commit();
//6、释放资源
sqlSession.close();
in.close();
}
二、删除----根据id删除
UerDao接口中:
public void deleteUser(Integer id);
映射配置文件UserDao.xml中:
<delete id="deleteUser" parameterType="java.lang.Integer">
delete from user where id=#{id};
</delete>
测试类:
@Test
public void testDelete() throws IOException {
//1、读取配置文件,生成字节输入流
InputStream in= Resources.getResourceAsStream("rk.xml");
//2、获取sqlSessionfactory
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
//3、获取sqlsession对象
SqlSession sqlSession = factory.openSession();
//4、获取dao的代理对象
UserDao userDao = sqlSession.getMapper(UserDao.class);
//执行修改操作
userDao.deleteUser(41);
//提交事务
sqlSession.commit();
//6、释放资源
sqlSession.close();
in.close();
}
三、修改
UerDao接口中:
public void updateUser(User user);
映射配置文件UserDao.xml中:
<update id="updateUser" parameterType="eud.rk.domain.User">
update user set username=#{username},address=#{address},sex=#{sex},birthday=#{birthday} where id=#{id};
</update>
测试类:
@Test
public void testUpdate() throws IOException {
User user=new User();
user.setUsername("李四");
user.setAddress("陕西省宝鸡市");
user.setSex("女");
user.setId(41);
user.setBirthday(new Date());
//1、读取配置文件,生成字节输入流
InputStream in= Resources.getResourceAsStream("rk.xml");
//2、获取sqlSessionfactory
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
//3、获取sqlsession对象
SqlSession sqlSession = factory.openSession();
//4、获取dao的代理对象
UserDao userDao = sqlSession.getMapper(UserDao.class);
//执行修改操作
userDao.updateUser(user);
//提交事务
sqlSession.commit();
//6、释放资源
sqlSession.close();
in.close();
}
四、查询
UerDao接口中:
查询所有:
public List<User> findAll();
查询一个:
public User findById(Integer id);
映射配置文件UserDao.xml中:
<select id="findAll" resultType="eud.rk.domain.User">
select * from user;
</select>
<select id="findById" resultType="eud.rk.domain.User">
select * from user where id=#{id};
</select>
测试类:
@Test
public void testFindAll() throws IOException {
//1、读取配置文件,生成字节输入流
InputStream in= Resources.getResourceAsStream("rk.xml");
//2、获取sqlSessionfactory
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
//3、获取sqlsession对象
SqlSession sqlSession = factory.openSession();
//4、获取dao的代理对象
UserDao userDao = sqlSession.getMapper(UserDao.class);
//5、执行查询所有的方法
List<User> users = userDao.findAll();
for (User user : users) {
System.out.println(user);
}
sqlSession.close();
in.close();
}
@Test
public void testFindbyId() throws IOException {
//1、读取配置文件,生成字节输入流
InputStream in= Resources.getResourceAsStream("rk.xml");
//2、获取sqlSessionfactory
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
//3、获取sqlsession对象
SqlSession sqlSession = factory.openSession();
//4、获取dao的代理对象
UserDao userDao = sqlSession.getMapper(UserDao.class);
User user = userDao.findById(52);
System.out.println(user);
//6、释放资源
sqlSession.close();
in.close();
}
模糊查询
UerDao接口中:
List<User> findByName(String username);
映射配置文件UserDao.xml中:
<select id="findByName" resultType="eud.rk.domain.User" parameterType="String">
select * from user where username like #{name}
</select>
查询语句也可以替换成: select * from user where username like '%{value}%'
测试类:
public void testFindbyUsername() throws IOException {
//1、读取配置文件,生成字节输入流
InputStream in= Resources.getResourceAsStream("rk.xml");
//2、获取sqlSessionfactory
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
//3、获取sqlsession对象
SqlSession sqlSession = factory.openSession();
//4、获取dao的代理对象
UserDao userDao = sqlSession.getMapper(UserDao.class);
List<User> users = userDao.findByName("%王%");
for (User user : users) {
System.out.println(user);
}
//6、释放资源
sqlSession.close();
in.close();
}
查询总记录条数:
UerDao接口中:
public int findTotal();
映射配置文件UserDao.xml中:
<select id="findTotal" resultType="int">
select count(id) from user;
</select>
测试类:
@Test
public void testFindTotal() throws IOException {
//1、读取配置文件,生成字节输入流
InputStream in= Resources.getResourceAsStream("rk.xml");
//2、获取sqlSessionfactory
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
//3、获取sqlsession对象
SqlSession sqlSession = factory.openSession();
//4、获取dao的代理对象
UserDao userDao = sqlSession.getMapper(UserDao.class);
int total = userDao.findTotal();
System.out.println(total);
//6、释放资源
sqlSession.close();
in.close();
}
OGNL表达式:它是通过对象的取值来获取数据。在写法上把get给省略了
根据QueryVo的条件查询模糊用户
QueryVo对象:
public class QueryVo {
private User user=new User();
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
UerDao接口中:
public List<User> findUserByVo(QueryVo vo);
映射配置文件UserDao.xml中:
<select id="findUserByVo" parameterType="eud.rk.domain.QueryVo" resultType="eud.rk.domain.User">
select * from user where username like #{user.username};
</select>
测试类:
@Test
public void testFindByVo() throws IOException {
QueryVo vo=new QueryVo();
User user = vo.getUser();
user.setUsername("%王%");
vo.setUser(user);
//1、读取配置文件,生成字节输入流
InputStream in= Resources.getResourceAsStream("rk.xml");
//2、获取sqlSessionfactory
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
//3、获取sqlsession对象
SqlSession sqlSession = factory.openSession();
//4、获取dao的代理对象
UserDao userDao = sqlSession.getMapper(UserDao.class);
List<User> users = userDao.findUserByVo(vo);
for (User u : users) {
System.out.println(u);
}
//6、释放资源
sqlSession.close();
in.close();
}