雄鹰展翅凌空飞, 大江奔流不回头。 壮志未酬心未老, 豪情万丈任遨游。
巍巍高山攀顶峰, 滔滔黄河入海流。 风云变幻凭君舞, 踏遍天涯尽逍遥。
目录
一,环境准备
基于上一篇文章的环境Java---Mybatis详解一-CSDN博客 ,新增加一些数据:
INSERT INTO tb_user (username, password) VALUES ('user1', 'password123');
INSERT INTO tb_user (username, password) VALUES ('user2', 'password1233');
INSERT INTO tb_user (username, password) VALUES ('user3', 'password1234');
INSERT INTO tb_user (username, password) VALUES ('user4', 'password12345');
INSERT INTO tb_user (username, password) VALUES ('user5', 'password123456');
在单元测试中通过IOC依赖注入,来查询所插入的数据:
@SpringBootTest
class Demo1ApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void test() {
List<UserBean> userBeanList = userMapper.findAll();
userBeanList.stream().forEach(userBean -> {
System.out.println(userBean.toString());
});
}
}
输出结果:
至此,环境准备完毕
二,删除
要进行删除操作,首先要在之前创建的UserMapper中添加一个删除的接口:
@Mapper
public interface UserMapper {
@Select("select * from tb_user")
public List<UserBean> findAll();
//根据id删除数据
@Delete("delete from tb_user where id =#{id}")
public void deleteById(Integer id);
}
注意:因为id是我们传入的参数,所以需要动态获取,springboot中使用#{id}来表示动态传入的参数
在单元测试中根据依赖注入来调用删除方法:
@SpringBootTest
class Demo1ApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void test() {
List<UserBean> userBeanList = userMapper.findAll();
userBeanList.stream().forEach(userBean -> {
System.out.println(userBean.toString());
});
}
@Test
public void testDel() {
userMapper.deleteById(2);
}
}
运行testDel方法后,查看数据库:
发现数据库中少了id为2的数据,所以删除成功了。
三,删除(预编译SQL)
在springboot中配置mybatis的日志,可以指定输出日志到控制台。
在application.properties中添加配置项:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/yz
spring.datasource.username=root
spring.datasource.password=1234
#配置mybatis的日志,指定输出到控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
然后执行删除操作:
@SpringBootTest
class Demo1ApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void test() {
List<UserBean> userBeanList = userMapper.findAll();
userBeanList.stream().forEach(userBean -> {
System.out.println(userBean.toString());
});
}
@Test
public void testDel() {
userMapper.deleteById(3);
}
}
然后我们就会在控制台看到删除的相关sql语句的日志:
预编译SQL有什么优势呢?
为什么性能更高?
先看下没有预编译的情况:
每执行一条sql语句,需要执行三次缓存
再来看一下预编译的情况:
通过预编译sql语句,只需要缓存一次即可,性能更高
为什么更安全?
先来看看什么是SQL注入:
SQL注入,是通过操作输入的数据来修改事先定义好的SQL语句,以达到执行代码对服务器进行攻击的方法。
预编译会将输入的内容整个的替换为? 从而防止SQL注入。
四,新增
新增数据同样需要现在UserMapper中增加新增的接口
@Mapper
public interface UserMapper {
@Select("select * from tb_user")
public List<UserBean> findAll();
//根据id删除数据
@Delete("delete from tb_user where id =#{id}")
public void deleteById(Integer id);
//插入数据
@Insert("insert into tb_user(username,password)"+" values (#{username},#{password})")
public void insert(UserBean user);
}
在application.properties中添加插入测试方法:
@SpringBootTest
class Demo1ApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void test() {
List<UserBean> userBeanList = userMapper.findAll();
userBeanList.stream().forEach(userBean -> {
System.out.println(userBean.toString());
});
}
@Test
public void testDel() {
userMapper.deleteById(3);
}
@Test
public void testInsert(){
UserBean userBean = new UserBean();
userBean.setUsername("袁震");
userBean.setPassword("123456");
userMapper.insert(userBean);
}
}
运行之后,查看数据库表:
插入操作就完成了。
五,更新
更新数据同样需要在UserMapper中添加接口:
@Mapper
public interface UserMapper {
@Select("select * from tb_user")
public List<UserBean> findAll();
//根据id删除数据
@Delete("delete from tb_user where id =#{id}")
public void deleteById(Integer id);
//插入数据
@Insert("insert into tb_user(username,password) values (#{username},#{password})")
public void insert(UserBean user);
//更新用户信息
@Update("update tb_user set username =#{username},password =#{password} where id =#{id}")
public void upDate(UserBean user);
}
在测试程序中添加测试代码:
@SpringBootTest
class Demo1ApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void test() {
List<UserBean> userBeanList = userMapper.findAll();
userBeanList.stream().forEach(userBean -> {
System.out.println(userBean.toString());
});
}
@Test
public void testDel() {
userMapper.deleteById(3);
}
@Test
public void testInsert(){
UserBean userBean = new UserBean();
userBean.setUsername("袁震");
userBean.setPassword("123456");
userMapper.insert(userBean);
}
@Test
public void testUpdate(){
UserBean userBean = new UserBean();
userBean.setId(6);
userBean.setPassword("123456789");
userBean.setUsername("袁震1");
userMapper.upDate(userBean);
}
}
运行后观察数据库:
修改成功
六,查询
在UserMapper中添加根据id查询用户信息接口:
@Mapper
public interface UserMapper {
@Select("select * from tb_user")
public List<UserBean> findAll();
//根据id删除数据
@Delete("delete from tb_user where id =#{id}")
public void deleteById(Integer id);
//插入数据
@Insert("insert into tb_user(username,password) values (#{username},#{password})")
public void insert(UserBean user);
//更新用户信息
@Update("update tb_user set username =#{username},password =#{password} where id =#{id}")
public void upDate(UserBean user);
//根据id查询用户信息
@Select("select * from tb_user where id =#{id}")
public UserBean findById(Integer id);
}
在测试程序中测试查询结果:
@SpringBootTest
class Demo1ApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void test() {
List<UserBean> userBeanList = userMapper.findAll();
userBeanList.stream().forEach(userBean -> {
System.out.println(userBean.toString());
});
}
@Test
public void testDel() {
userMapper.deleteById(3);
}
@Test
public void testInsert(){
UserBean userBean = new UserBean();
userBean.setUsername("袁震");
userBean.setPassword("123456");
userMapper.insert(userBean);
}
@Test
public void testUpdate(){
UserBean userBean = new UserBean();
userBean.setId(6);
userBean.setPassword("123456789");
userBean.setUsername("袁震1");
userMapper.upDate(userBean);
}
@Test
public void testFind(){
UserBean userBean = userMapper.findById(6);
System.out.println(userBean.toString());
}
}
输出:
注意:
解决方法:
七,条件查询
like 模糊匹配
在UserMapper中添加接口:
@Mapper
public interface UserMapper {
@Select("select * from tb_user")
public List<UserBean> findAll();
//根据id删除数据
@Delete("delete from tb_user where id =#{id}")
public void deleteById(Integer id);
//插入数据
@Insert("insert into tb_user(username,password) values (#{username},#{password})")
public void insert(UserBean user);
//更新用户信息
@Update("update tb_user set username =#{username},password =#{password} where id =#{id}")
public void upDate(UserBean user);
//根据id查询用户信息
@Select("select * from tb_user where id =#{id}")
public UserBean findById(Integer id);
//条件查询用户信息 查询包含user的用户
@Select("select * from tb_user where username like concat('%',#{name},'%')")
public List<UserBean> findByName(String name);
}
在测试程序中测试:
@SpringBootTest
class Demo1ApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void test() {
List<UserBean> userBeanList = userMapper.findAll();
userBeanList.stream().forEach(userBean -> {
System.out.println(userBean.toString());
});
}
@Test
public void testDel() {
userMapper.deleteById(3);
}
@Test
public void testInsert(){
UserBean userBean = new UserBean();
userBean.setUsername("袁震");
userBean.setPassword("123456");
userMapper.insert(userBean);
}
@Test
public void testUpdate(){
UserBean userBean = new UserBean();
userBean.setId(6);
userBean.setPassword("123456789");
userBean.setUsername("袁震1");
userMapper.upDate(userBean);
}
@Test
public void testFind(){
UserBean userBean = userMapper.findById(6);
System.out.println(userBean.toString());
}
@Test
public void testFindByName(){
List<UserBean> user = userMapper.findByName("user");
System.out.println(user.toString());
}
}
输出:
这样所有包含user的用户都查询出来了。