Mybatis的CRUD操作
1. select
- select标签是mybatis中最常用的标签之一
- select语句有很多属性可以详细配置每一条SQL语句
- id
- 命名空间中唯一的标识符
- 接口中的方法名与映射文件中的SQL语句ID 一一对应
- parameterType
- 传入SQL语句的参数类型 。【万能的Map,可以多尝试使用】
- resultType
- SQL语句返回值类型。【完整的类名或者别名】
- id
案例: 根据id查询用户
- 在UserMapper中添加方法:
public interface UserMapper {
List<User> getUserList();
//根据id查询用户
User findById(@Param("id") int id);
}
- 在UserMapper.xml中添加Select语句
<select id="findById" resultType="com.ljs.mybatis.pojo.User">
select * from user where id=#{id}
</select>
- 测试类中测试
@Test
public void getUserById() {
SqlSession session = MybatisUtils.getSessionFactory();
UserMapper mapper = session.getMapper(UserMapper.class);
User user = mapper.findById(1);
System.out.println(user);
}
可以根据 密码 和 名字 查询用户
- 思路一:直接在方法中传递参数
User findByNameAndPwd(@Param("name") String name, @Param("pwd") String paasword);
-
思路二:使用万能的Map
- 在接口方法中,参数直接传递Map;
List<User> getListByNameAndPwd2(Map<String,Object> map);
- 编写sql语句的时候,需要传递参数类型,参数类型为map
<select id="getListByMap" parameterType="map" resultType="com.ljs.mybatis.pojo.User"> select * from user where name=#{name} and pwd=#{pwd} </select>
- 在使用方法的时候,Map的 key 为 sql中取的值即可,没有顺序要求!
Map<String, Object> map = new HashMap<String, Object>(); map.put("name", "狂神"); map.put("pwd", "123456"); User user2 = mapper.findByNameAndPwd2(map);
总结: 如果参数过多,我们可以考虑直接使用Map实现,如果参数比较少,直接传递参数即可
2. insert
我们一般使用insert标签进行插入操作,它的配置和select标签差不多!
需求:给数据库增加一个用户
- 在UserMapper接口中添加对应的方法
int insert(User user);
- 在UserMapper.xml中添加insert语句
<insert id="insert" parameterType="com.ljs.mybatis.pojo.User">
insert into user(id,name,pwd) values(#{id}, #{name}, #{pwd})
</insert>
- 测试
@Test
public void insert() {
User user = new User(4, "张无忌", "zhangwuji");
SqlSession session = MybatisUtils.getSessionFactory();
UserMapper mapper = session.getMapper(UserMapper.class);
int insert = mapper.insert(user);
System.out.println(insert);
session.commit();//提交事务,重点!不写的话不会提交到数据库
session.close();
}
注意点:增、删、改操作需要提交事务!
3 update
需求:修改用户的信息
- 编写接口方法
int updateUser(@Param("id")int id, @Param("pwd") String pwd);
int updateByUser(User user);
- 编写对应的配置文件SQL
<update id="updateUser" parameterType="string">
update user set pwd = #{pwd} where id = #{id}
</update>
<update id="updateUser" parameterType="com.ljs.mybatis.pojo.User">
update user set pwd = #{pwd}, name= #{name} where id = #{id}
</update>
- 测试
@Test
public void update() {
SqlSession session = MybatisUtils.getSessionFactory();
UserMapper mapper = session.getMapper(UserMapper.class);
User user = mapper.findById(1);
user.setPwd("112233");
int updateByUser = mapper.updateByUser(user);
int updateUser = mapper.updateUser(2, "222222");
System.out.println("updateUser==>>" + updateUser);
System.out.println("updateByUser==>>" + updateByUser);
session.commit();//提交事务,重点!不写的话不会提交到数据库
session.close();
}
4. delete
根据id删除一个用户
- 编写接口方法
int deleteById(String id);
- 编写对应的配置文件SQL
<delete id="deleteById" parameterType="int">
delete from user where id = #{id}
</delete>
- 测试
@Test
public void delete() {
SqlSession session = MybatisUtils.getSessionFactory();
UserMapper mapper = session.getMapper(UserMapper.class);
int ok = mapper.deleteById(5);
}
5.模糊查询like语句
- 第1种:在sql语句中拼接通配符,会引起sql注入
List<User> getListByParams(@Param("name") String name);
<select id="getListByParams" resultType="com.ljs.mybatis.pojo.User">
select * from user where name like "%"#{name}"%"
</select>
- 第2种:在Java代码中添加sql通配符。
String name2 = "%"+name+"%";
List<User> getListByParams2(@Param("name") String name2);
<select id="getListByParams2" resultType="com.ljs.mybatis.pojo.User">
select * from user where name like #{name}
</select>
排错:
-
标签必须比配一致
-
mybatis-config.xml的resource路径和mapper.xml中namespace路径
<mappers> <mapper resource="com/ljs/mybatis/dao/xml/UserMapper.xml"/> </mappers> <mapper namespace="com.ljs.mybatis.dao.UserMapper">
-
maven资源没有导出问题
向pom.xml中添加
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
小结:
- 所有的增删改操作都需要提交事务!
- 接口所有的普通参数,尽量都写上@Param参数,尤其是多个参数时,必须写上!
- 有时候根据业务的需求,可以考虑使用map传递参数!
- 为了规范操作,在SQL的配置文件中,我们尽量将Parameter参数和resultType都写上!
掌握:
- 将数据库配置文件外部引入
- 实体类别名
- 保证UserMapper接口和UserMapper.xml改为一致! 并且放在同一个报下!