一、使用MyBatis对表执行CRUD操作
- 首先在UsersMapper接口中编写相应的方法
package mybatis.Mapper;
import mybatis.pojo.Users;
import java.util.List;
public interface UsersMapper {
public List<Users> getAll();
public Users getById(int id);
public List<Users> getByUserName(String name);
public int save(Users u);
public int delete(Integer id);
public int update(Users u);
}
- 完成UsersMapper.xml文件内具体方法内容的编写
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
1、Mapper.xml文件中的namespace与mapper接口的类路径相同。
2、Mapper接口方法名和Mapper.xml中定义的每个statement的id相同
3、Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql 的parameterType的类型相同
4、Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同
5、接口与xml文件必同名,而且在同一个路径下
-->
<mapper namespace="mybatis.Mapper.UsersMapper">
<sql id="allcolumn">
id,username,age
</sql>
<select id="getAll" resultType="mybatis.pojo.Users">
select <include refid="allcolumn"></include>
from users
</select>
<select id="getById" parameterType="int" resultType="mybatis.pojo.Users">
select <include refid="allcolumn"></include>
from users where id=#{id}
</select>
<select id="getByUserName" parameterType="string" resultType="mybatis.pojo.Users">
select <include refid="allcolumn"></include>
from users
where username like '%${value}%'
</select>
<!--增加用户-->
<insert id="save" parameterType="mybatis.pojo.Users">
insert into users(username, age) values (#{username},#{age})
</insert>
<!--删除用户-->
<delete id="delete" parameterType="int">
delete from users where id=#{id}
</delete>
<!--更新用户-->
<update id="update" parameterType="mybatis.pojo.Users">
update users set username=#{username},age=#{age}
where id=#{id}
</update>
</mapper>
- 编写测试类代码
package mybatis.Test;
import mybatis.Mapper.UsersMapper;
import mybatis.pojo.Users;
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.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
public class MyTest {
SqlSession sqlSession;
UsersMapper usersMapper;
@Before
public void setSession() throws Exception{
InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
sqlSession=factory.openSession();
usersMapper=sqlSession.getMapper(UsersMapper.class);
}
@Test
public void testgetAll(){
List<Users> ulist = usersMapper.getAll();
for (Users u:ulist){
System.out.println(u);
}
sqlSession.close();
}
@Test
public void testGetById(){
Users u=usersMapper.getById(1);
System.out.println(u);
sqlSession.close();
}
@Test
public void testfindUsersByName(){
List<Users> ulist = usersMapper.getByUserName("三");
for(Users u:ulist){
System.out.println(u);
}
sqlSession.close();
}
@Test
public void testSave(){
Users u=new Users();
u.setUsername("王五");
u.setAge(15);
int num=usersMapper.save(u);
sqlSession.commit();
sqlSession.close();
System.out.println(num);
}
@Test
public void testDelete(){
int num=usersMapper.delete(1);
sqlSession.commit();
sqlSession.close();
System.out.println(num);
}
@Test
public void testUpdate(){
Users u=new Users();
u.setUsername("赵六");
u.setAge(12);
u.setId(5);
int num=usersMapper.update(u);
sqlSession.commit();
sqlSession.close();
System.out.println(num);
}
}
- 运行结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3ff1d327cf205f750b7f039fe88f7f99.png)