Step 1: Write the interface class in Dao
public interface UserMapper {
//查询全部用户
List<User> getUsers();
//根据ID查询用户
User getUserById(int id);
//插入一个用户
int addUser(User user);
//修改用户
int updataUser(User user);
//删除用户
int deleteUser(int id);
}
Step 2: Bind the interface in Mapper and implement it
<?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">
<!--namespace绑定一个对应Dao/Mapper的接口-->
<mapper namespace="com.LinXiaoDe.dao.UserMapper">
<!-- 查询全部用户-->
<select id="getUsers" resultType="com.LinXiaoDe.pojo.User">
select * from mybatis_db.user_t
</select>
<!--根据ID查询用户-->
<select id="getUserById" parameterType="int" resultType="com.LinXiaoDe.pojo.User">
select * from mybatis_db.user_t where id=#{id}
</select>
<!--插入一个用户-->
<insert id="addUser" parameterType="com.LinXiaoDe.pojo.User">
insert into mybatis_db.user_t (id,userName,password) value (#{id},#{userName},#{password})
</insert>
<!--修改用户-->
<update id="updataUser" parameterType="com.LinXiaoDe.pojo.User">
update mybatis_db.user_t set id=#{id},userName=#{userName},password=#{password} where id=#{id}
</update>
<!--删除用户-->
<delete id="deleteUser" parameterType="int">
delete from mybatis_db.user_t where id=#{id}
</delete>
</mapper>
Step 3: Call the interface in JUnity and commit the transaction
- Note that the transaction must be submitted, otherwise it will not take effect
public class test {
@Test
public void test1(){
SqlSession sqlSession = null;
try{
//第一步,从工具类中获取sqlSession对象
sqlSession=MyBatisUtil.getSqlSession();
//第二步,执行SQL
UserMapper mapper= sqlSession.getMapper(UserMapper.class);
List<User> users= mapper.getUsers();
//第三步遍历
for (User user:users){
System.out.println(user);
}
}catch (Exception e){
e.printStackTrace();
}finally {
//关闭sqlSession
sqlSession.close();
}
}
@Test
public void test2(){
SqlSession sqlSession=MyBatisUtil.getSqlSession();
UserMapper mapper= sqlSession.getMapper(UserMapper.class);
mapper.addUser(new User(4,"EE","E1243"));
sqlSession.commit();//注意一定要进行事务提交,否则不会生效
sqlSession.close();
}
}
Omnipotent map
In actual projects, the attribute values of a piece of data in the database may be very, very many, but if CRUD operations only modify a certain attribute of a piece of information, it will be very complicated if the above method is still used. For example, if you want to modify data, you must pass in a User object every time. This object has a lot of properties. If you construct all of it, it will take a lot of effort. At this time, you can consider using the Map method mentioned below:
(1) mapper interface
//userMapper.java
public interface UserMapper {
int addUser(User user);
//修改用户
int updataUser(User user);
//map方法修改用户
int updataUserMap(Map<String,Object>map);
}
(2) In the mapper
<!--修改用户-->
<update id="updataUser" parameterType="com.LinXiaoDe.pojo.User">
update mybatis_db.user_t set id=#{
id},userName=#{
userName},password=#{
password} where id=#{
id}
</update>
这里#{
}中的属性名可以不和数据库中的一致,只需要用map<key,value>对应即可
<!--修改用户map-->
<insert id="updataUserMap" parameterType="map">
update mybatis_db.user_t set userName=#{
name},password=#{
pwd} where id=#{
userId}
(3) Test
@Test
public void updataUserMap()
{
SqlSession sqlSession = MyBatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Map<String,Object> map=new HashMap<String, Object>();
map.put("userId",5);
map.put("name","map");
map.put("pwd","000000");
mapper.updataUserMap(map);
sqlSession.commit();
}
Fuzzy query
- Fuzzy query is based on the value of the query, use the like keyword in the SQL statement
- Note that the query keywords
#{%value%}
should be spliced with wildcards - Pay attention to hard-coded input strings to prevent SQL injection
(1) mapper interface
//模糊查询
List<User> getUserLike(String value);
(2)mapper.xml
<select id="getUserLike" parameterType="String" resultType="com.LinXiaoDe.pojo.User">
select * from mybatis_db.user_t where userName like #{
value}
</select>
(3) Test
@Test
public void getUserLike(){
SqlSession sqlSession = MyBatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> users = mapper.getUserLike("%抑%");
for (User user:users){
System.out.println(user);
}
sqlSession.commit();
sqlSession.close();
}