spring -- 7、springAOP增删改查(利用JdbcTemplate实现增删改查)

快来打我* 2023-02-24 08:37 97阅读 0赞

目录

利用JdbcTemplate实现增删改查:

1.插入

2.批量插入

3.删

4.更

5.查询某个值,并以对象的方式返回

6.查询返回集合对象

7.返回组合函数的值

8.使用具备具名函数的JdbcTemplate

9.整合EmpDao

10.也可以去官方API文档


利用JdbcTemplate实现增删改查:

1、将上一个项目的pom.xml文件内的插件复制过来:

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.mashibing</groupId>
  7. <artifactId>spring_demo</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <dependencies>
  10. <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
  11. <dependency>
  12. <groupId>org.springframework</groupId>
  13. <artifactId>spring-context</artifactId>
  14. <version>5.2.3.RELEASE</version>
  15. </dependency>
  16. <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
  17. <dependency>
  18. <groupId>com.alibaba</groupId>
  19. <artifactId>druid</artifactId>
  20. <version>1.1.21</version>
  21. </dependency>
  22. <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  23. <dependency>
  24. <groupId>mysql</groupId>
  25. <artifactId>mysql-connector-java</artifactId>
  26. <version>5.1.47</version>
  27. </dependency>
  28. <!-- https://mvnrepository.com/artifact/cglib/cglib -->
  29. <dependency>
  30. <groupId>cglib</groupId>
  31. <artifactId>cglib</artifactId>
  32. <version>3.3.0</version>
  33. </dependency>
  34. <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
  35. <dependency>
  36. <groupId>org.aspectj</groupId>
  37. <artifactId>aspectjweaver</artifactId>
  38. <version>1.9.5</version>
  39. </dependency>
  40. <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
  41. <dependency>
  42. <groupId>aopalliance</groupId>
  43. <artifactId>aopalliance</artifactId>
  44. <version>1.0</version>
  45. </dependency>
  46. <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
  47. <dependency>
  48. <groupId>org.springframework</groupId>
  49. <artifactId>spring-aspects</artifactId>
  50. <version>5.2.3.RELEASE</version>
  51. </dependency>
  52. </dependencies>
  53. </project>

并且导入:

  1. <dependency>
  2. <groupId>org.springframework</groupId>
  3. <artifactId>spring-orm</artifactId>
  4. <version>5.2.3.RELEASE</version>
  5. </dependency>

2、再添加MySQL和Druid(阿里巴巴)

3、

20200708231333103.png

4、创建:

20200708231345568.png

db.properties

  1. jdbc.username=root
  2. jdbc.password=123456
  3. jdbc.url=jdbc:mysql://localhost:3306/deom //deom是数据库名称
  4. jdbc.driverName=com.mysql.jdbc.Driver

xml**文件:applicationContext** 20200708231436449.png

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="
  3. http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:aop="http://www.springframework.org/schema/aop"
  8. xsi:schemaLocation="
  9. http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx.xsd
  15. http://www.springframework.org/schema/aop
  16. https://www.springframework.org/schema/aop/spring-aop.xsd">
  17. <context:component-scan base-package="com.mashibing"></context:component-scan> //组件扫描
  18. <context:property-placeholder location="classpath:db.properties">
  19. </context:property-placeholder>
  20. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  21. <property name="username" value="${jdbc.username}"></property>
  22. <property name="password" value="${jdbc.password}"></property>
  23. <property name="driverClassName" value="${jdbc.driverName}"></property>
  24. <property name="url" value="${jdbc.url}"></property>
  25. </bean>

20200708231635773.png

  1. //jdbcTemplate注册为bean对象
  2. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  3. <property name="dataSource" ref="dataSource"></property>
  4. </bean>

20200708231703184.png

  1. //配置事务管理器的bean对象
  2. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  3. <property name="dataSource" ref="dataSource"></property>
  4. </bean>
  5. //开启基于注解的事务管理器的配置
  6. <tx:annotation-driven transaction-manager="transactionManager">
  7. </tx:annotation-driven>
  8. </beans>

java**文件:MyTest(测试)**

  1. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext
  2. ("applicationContext.xml");
  3. @Test
  4. public void test01() throws SQLException {
  5. DruidDataSource dataSource = context.getBean("dataSource", DruidDataSource.class);
  6. System.out.println(dataSource.getConnection());
  7. JdbcTemplate jdbcTemplate = context.getBean("jdbcTemplate", JdbcTemplate.class);
  8. System.out.println(jdbcTemplate);
  9. }

20200708231837261.png

1.插入

  1. @Test //增
  2. public void test02(){
  3. JdbcTemplate jdbcTemplate = context.getBean("jdbcTemplate", JdbcTemplate.class);
  4. String sql = "insert into emp(empno,ename) values(?,?)";
  5. int zhangsan = jdbcTemplate.update(sql, 1111, "zhangsan");
  6. System.out.println(zhangsan);
  7. }

20200708231905280.png

2.批量插入

  1. @Test //批量增加
  2. public void test03(){
  3. JdbcTemplate jdbcTemplate = context.getBean("jdbcTemplate", JdbcTemplate.class);
  4. String sql = "insert into emp(empno,ename) values(?,?)";
  5. List<Object[]> list = new ArrayList<Object[]>();
  6. list.add(new Object[]{2222,"lisi"});
  7. list.add(new Object[]{3333,"wangwu"});
  8. list.add(new Object[]{4444,"maliu"});
  9. int[] result = jdbcTemplate.batchUpdate(sql,list);
  10. for (int i : result) {
  11. System.out.println(i);
  12. }
  13. }

20200708231933706.png20200708231937569.png

3.删

  1. @Test //删
  2. public void test04(){
  3. JdbcTemplate jdbcTemplate = context.getBean("jdbcTemplate", JdbcTemplate.class);
  4. String sql = "delete from emp where empno = ?";
  5. int zhangsan = jdbcTemplate.update(sql, 1111);
  6. System.out.println(zhangsan);
  7. }

20200708232008622.png

4.更

  1. @Test //改
  2. public void test05(){
  3. JdbcTemplate jdbcTemplate = context.getBean("jdbcTemplate", JdbcTemplate.class);
  4. String sql = "update emp set ename=? where empno=?";
  5. int wangwu= jdbcTemplate.update(sql,"mashibing",2222);
  6. System.out.println(wangwu);
  7. }

20200708232044855.png

5.查询某个值,并以对象的方式返回

java**文件:**Emp(容器) 20200708232052272.png

  1. public class Emp {
  2. private Integer empno;
  3. private String ename;
  4. private String job;
  5. private Integer mgr;
  6. private Date hiredate;
  7. private Double sal;
  8. private Double comm;
  9. private Integer deptno;
  10. public Emp() {
  11. }
  12. public Emp(Integer empno, String ename) {
  13. this.empno = empno;
  14. this.ename = ename;
  15. }
  16. public Integer getEmpno() {return empno;}
  17. public void setEmpno(Integer empno) {this.empno = empno;}
  18. public String getEname() {return ename;}
  19. public void setEname(String ename) {this.ename = ename;}
  20. public String getJob() {return job;}
  21. public void setJob(String job) {this.job = job;}
  22. public Integer getMgr() {return mgr;}
  23. public void setMgr(Integer mgr) {this.mgr = mgr;}
  24. public Date getHiredate() {return hiredate;}
  25. public void setHiredate(Date hiredate) {this.hiredate = hiredate;}
  26. public Double getSal() {return sal;}
  27. public void setSal(Double sal) {this.sal = sal;}
  28. public Double getComm() {return comm;}
  29. public void setComm(Double comm) {this.comm = comm;}
  30. public Integer getDeptno() {return deptno;}
  31. public void setDeptno(Integer deptno) {this.deptno = deptno;}
  32. @Override
  33. public String toString() {
  34. return "Emp{" +
  35. "empno=" + empno +
  36. ", ename='" + ename + '\'' +
  37. ", job='" + job + '\'' +
  38. ", mgr=" + mgr +
  39. ", hiredate=" + hiredate +
  40. ", sal=" + sal +
  41. ", comm=" + comm +
  42. ", deptno=" + deptno +
  43. '}';
  44. }
  45. }

java**文件:MyTest(测试)**

  1. @Test //查
  2. public void test06(){
  3. JdbcTemplate jdbcTemplate = context.getBean("jdbcTemplate", JdbcTemplate.class);
  4. String sql = "select * from emp where empno=?";
  5. Emp result = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(Emp.class),7369);
  6. System.out.println(result);
  7. }

20200708232154766.png

6.查询返回集合对象

  1. @Test //查询所有
  2. public void test07(){
  3. JdbcTemplate jdbcTemplate = context.getBean("jdbcTemplate", JdbcTemplate.class);
  4. String sql = "select * from emp where sal >?";
  5. List<Emp> result = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Emp.class),1500);
  6. for (Emp emp : result) {
  7. System.out.println(emp);
  8. }
  9. }

7.返回组合函数的值

MyTest.java

  1. public class MyTest {
  2. public static void main(String[] args) throws SQLException {
  3. ApplicationContext context = new ClassPathXmlApplicationContext("jdbcTemplate.xml");
  4. JdbcTemplate jdbcTemplate = context.getBean("jdbcTemplate", JdbcTemplate.class);
  5. String sql = "select max(sal) from emp";
  6. Double aDouble = jdbcTemplate.queryForObject(sql, Double.class);
  7. System.out.println(aDouble);
  8. }
  9. }

8.使用具备具名函数的JdbcTemplate

MyTest.java

  1. public class MyTest {
  2. public static void main(String[] args) throws SQLException {
  3. ApplicationContext context = new ClassPathXmlApplicationContext("jdbcTemplate.xml");
  4. NamedParameterJdbcTemplate jdbcTemplate = context.getBean("namedParameterJdbcTemplate", NamedParameterJdbcTemplate.class);
  5. String sql = "insert into emp(empno,ename) values(:empno,:ename)";
  6. Map<String,Object> map = new HashMap<>();
  7. map.put("empno",2222);
  8. map.put("ename","sili");
  9. int update = jdbcTemplate.update(sql, map);
  10. System.out.println(update);
  11. }
  12. }

9.整合EmpDao

添加:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTA0MjU2OQ_size_16_color_FFFFFF_t_70

java**文件:**EmpDao 20200708232257956.png

  1. @Repository仓库
  2. public class EmpDao {
  3. @Autowired
  4. private JdbcTemplate jdbcTemplate;
  5. public void save(Emp emp){
  6. String sql = "insert into emp(empno,ename) values(?,?)";
  7. int update = jdbcTemplate.update(sql,emp.getEmpno(),emp.getEname());
  8. System.out.println(update);
  9. }
  10. }

java**文件:MyTest(测试)**

  1. @Test //插入
  2. public void test08(){
  3. EmpDao empDao = context.getBean("empDao", EmpDao.class);
  4. empDao.save(new Emp(1111,"zhangsan"));
  5. }
  6. }

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTA0MjU2OQ_size_16_color_FFFFFF_t_70 120200815130813919.png

20200708232359416.png

10.也可以去官方API文档

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTA0MjU2OQ_size_16_color_FFFFFF_t_70 2

发表评论

表情:
评论列表 (有 0 条评论,97人围观)

还没有评论,来说两句吧...

相关阅读