增删改查
package com.wzb.MybatisImprove20240922;import com.wzb.Pojo20240922.Emp;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;@Mapper
public interface EmpMapper {// 用Mybatis操作数据库完成增、删、改、查// 写死的写法————一般不用// @Delete("delete from emp where id = 17")
// public void deleteEmp();/* 以上的delete操作的SQL语句的id写死成17了,所以说只能删除id=17的用户数据,但往往SQL语句中的id值是动态变化的 */// 解决方案:在delete方法和SQL语句中添加一个参数(用户id),将方法中的参数传递给SQL语句使用@Delete("delete from emp where id = #{id}")public void delete(Integer id);// 在Mybatis中,可以借助日志,查看SQL语句的执行、执行传递的参数和执行的结果// 操作:在application.properties文件中开启Mybatis日志,并指定其输出在控制台// mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl// 使用#{}的方式传递参数,其实是一种占位的操作,而这种SQL语句称为预编译SQL// 预编译SQL的优势:// 1.性能更高// 预编译SQL在编译一次之后,就会将编译后的SQL语句缓存起来,若后面再次执行到该语句时,无需再次编译,直接使用缓存的语句,只是参数不同// 2.更加安全(防止SQL注入)// 能够将敏感的语句进行转义,防止SQL注入// SQL注入:通过操作输入的数据来修改事先定义好的SQL语句,达到执行代码对服务器进行攻击的目的// SQL又是拼接而成,在用户输入参数时,在参数中添加一些SQL关键字,达到改变SQL运行结果的目的,也可以完成恶意攻击。// 通过Mybatis在数据库中增加数据
// @Insert("insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time)" +
// "values (#{username}, #{name}, #{gender}, #{image}, #{job}, #{entrydate}, #{deptId}, #{createTime}, #{updateTime})")
// // #{...}中填的内容是对象的属性名
// public void insert(Emp emp);// 主键返回:在数据成功添加之后,需要获取插入数据库数据的主键// 因为两张表若是多对多的关系,那么势必会有另一张表维护他们之间的关系,需要在一张表插入之后,返回其主键供第二步使用// 但是默认是不会返回主键的,若想返回主键,需要在Mapper接口中的方法加上@Options注解,并且在注解中指定属性:useGenerateKeys=true// 并且KeyProperty="实体类属性名"//会自动将生成的主键值,赋值给emp对象的id属性@Options(useGeneratedKeys = true, keyProperty = "id")@Insert("insert into emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time) " +"values (#{username}, #{name}, #{gender}, #{image}, #{job}, #{entrydate}, #{deptId}, #{createTime}, #{updateTime})")public void insert(Emp emp);
}
Test类
package com.wzb;import com.wzb.MybatisImprove20240922.EmpMapper;
import com.wzb.Pojo20240922.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.time.LocalDate;
import java.time.LocalDateTime;@SpringBootTest
class SpringbootExercise20240922ApplicationTests {
//
// @Autowired
// UserMapper userMapper;
//
// @Test
// public void userList() {
// List<User> users = userMapper.getList();
// for (User user : users) {
// System.out.println("这是Mybatis的输出" + user);
// }
// }
//
// @Test
// public void testJdbc() throws ClassNotFoundException, SQLException {
//
// // 1.注册驱动
// Class.forName("com.mysql.cj.jdbc.Driver"); // Mysql在JDBC的驱动
//
// // 2.获取数据库连接
// String url = "jdbc:mysql://127.0.0.1:3306/springboot";
// String username = "root";
// String password = "123456";
// Connection connection = DriverManager.getConnection(url, username, password);
//
// // 3.执行SQL语句
//
// // 获取操作SQL的对象
// Statement statement = connection.createStatement();
// String selectSql = "select * from user";
// // SQL查询的结果将封装到ResultSet对象中,需要在ResultSet对象中处理数据
// ResultSet rs = statement.executeQuery(selectSql);
// // 创建集合,用于存储User对象
// List<User> userList = new ArrayList<>();
//
// // 4.处理SQL执行的结果
//
// // 循环读取rs中从SQL返回的数据,只要还有数据,就继续读取
// while (rs.next()) {
// // 取出一行记录中id、name、age、gender、phone下的数据
// int id = rs.getInt("id");
// String name = rs.getString("name");
// short age = rs.getShort("age");
// short gender = rs.getShort("gender");
// String phone = rs.getString("phone");
// // 将一行记录中的数据封装到User对象中
// User user = new User(id, name, age, gender, phone);
// // 将User对象加入到集合中
// userList.add(user);
// }
//
// // 5.释放资源
// statement.close();
// connection.close();
// rs.close();
//
// // 使用集合
// for (User user : userList) {
// System.out.println("这是JDBC程序的输出" + user);
// }
// }// @Autowired // 从Spring的IOC容器中,获取类型是EmpMapper的对象注入
// private EmpMapper empMapper;
//
// @Test
// public void deleteEmp() {
//
// // 调用删除的方法
// empMapper.delete(16);
// }// @Autowired
// private EmpMapper empMapper;
//
// @Test
// public void insertEmp() {
// //创建员工对象
// Emp emp = new Emp();
// emp.setUsername("tom2");
// emp.setName("汤姆2");
// emp.setImage("1.jpg");
// emp.setGender((short)1);
// emp.setJob((short)1);
// emp.setEntrydate(LocalDate.of(2000,1,1));
// emp.setCreateTime(LocalDateTime.now());
// emp.setUpdateTime(LocalDateTime.now());
// emp.setDeptId(1);
// //调用添加方法
// empMapper.insert(emp);
// }@Autowiredprivate EmpMapper empMapper;@Testpublic void testInsert(){//创建员工对象Emp emp = new Emp();emp.setUsername("jack");emp.setName("杰克");emp.setImage("1.jpg");emp.setGender((short)1);emp.setJob((short)1);emp.setEntrydate(LocalDate.of(2000,1,1));emp.setCreateTime(LocalDateTime.now());emp.setUpdateTime(LocalDateTime.now());emp.setDeptId(1);//调用添加方法empMapper.insert(emp);System.out.println(emp.getDeptId());}
}