这篇文章主要介绍“怎么用Spring框架+jdbcTemplate实现增删改查功能”,在日常操作中,相信很多人在怎么用Spring框架+jdbcTemplate实现增删改查功能问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用Spring框架+jdbcTemplate实现增删改查功能”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
SpringMVC架构(Model(实体类),Service,Controller层)
Controller(接收参数调用业务层)–>Service(调用持久层,处理业务逻辑)–>Dao(与数据库交互)
DI(依赖注入):是IOC思想的一种技术实现
IOC容器是Spring提供的保存Bean对象的容器
Bean管理操作
1.Xml + 注解
2.javaConfig + 注解
通过xml配置Bean:TODO:
通过javaConfig 配置Bean:TODO:
通过注解配置Bean:TODO:
面向切面的程序设计思想。横向的调用。
eg:一个日志的功能,很多的功能模块都需要去使用,可以写一个切面去做这个事情。
使用@Aspect
来标记一个普通类为切面。
连接点:比如说日志需要作用的方法。
目标对象:日志需要使用的对象。
1. 添加依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.3.8</version> </dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.7</version> <scope>runtime</scope> </dependency>
需求:SpringIOC + JDBCTemplate实现简单的数据库操作
1.新建Maven项目并引入Spring核心4依赖
<!--Spring的4个基础jar包(容器包)--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.3.1</version> </dependency>
jdbc依赖
<!--Spring整合jdbc--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.6</version> </dependency> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency>
junit5
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.3.2</version> <scope>test</scope> </dependency>
2. 创建SpringConfig配置文件(通过JavaConfig方式注入bean)
创建SpringConfig
类,添加@Configuration
标记为配置类。
配置数据源和JDBCTemplate
的Bean
。
/** * @author YonC * @date 2021/9/2 */ @Configuration public class SpringConfig { @Bean public DataSource dataSource() { MysqlDataSource dataSource = new MysqlDataSource(); dataSource.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=ture&charactorEncoding=utf-8&serverTimezone=UTC"); dataSource.setUser("root"); dataSource.setPassword("123456"); return dataSource; } @Bean public JdbcTemplate jdbcTemplate(DataSource dataSource) { return new JdbcTemplate(dataSource); } }
3.创建MVC架构并创建与数据库字段对应的实体类对象
实体类:StudentPO
public class StudentPO { private Long id; private String name; private String age; public StudentPO() { } public StudentPO(String name, String age) { this.name = name; this.age = age; } public StudentPO(Long id, String name, String age) { this.id = id; this.name = name; this.age = age; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } @Override public String toString() { return "StudentPO{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; } }
4. 编写Dao层
面向接口编程,首先定义Dao层的规范接口,定义了增删改查4种方法
/** * @author YonC * @date 2021/9/2 */ public interface StudentDao { void addStudent(StudentPO student); void delStudentById(Long id); void updateStudent(StudentPO student); List<StudentPO> selectStudent(); }
接口的实现
@Repository
注解将StudentDao
注入IOC容器
@Autowired
自动装配JdbcTemplate
对象,JdbcTemplate
对象已经在SpringConfig
文件中实例化
/** * @author YonC * @date 2021/9/2 */ @Repository public class StudentDaoImpl implements StudentDao { @Autowired JdbcTemplate jdbcTemplate; /* * 增加Student * */ @Override public void addStudent(StudentPO student) { jdbcTemplate.update("insert into student (name,age) values (?,?)", student.getName(), student.getAge()); } /* * 删除Student * */ @Override public void delStudentById(Long id) { jdbcTemplate.update("delete from student where id=?", id); } /* * 修改Student * */ @Override public void updateStudent(StudentPO student) { String sql = "UPDATE student SET name=?,age=? where id = ? "; Object[] args = {student.getName(), student.getAge(), student.getId()}; jdbcTemplate.update(sql, args); } /* * 查询 * */ @Override public List<StudentPO> selectStudent() { String sql = "select id,name,age from student"; return this.jdbcTemplate.query(sql, (rs, index) -> { StudentPO student = new StudentPO(); student.setId(rs.getLong("id")); student.setName(rs.getString("name")); student.setAge(rs.getString("age")); return student; }); } }
5. Dao与数据库的增删改查已经实现,使用Service层去调用Dao层的方法。
首先定义Service层的接口
/** * @author YonC * @date 2021/9/2 */ public interface StudentService { void addStudent(StudentPO student); void delStudentById(Long id); void updateStudent(StudentPO student); List<StudentPO> selectStudent(); }
接口实现
@Service
将对象声明IOC容器中
@Autowired
自动装配IOC容器中的StudentDao
将StudentDao
对象初始化
/** * @author YonC * @date 2021/9/2 */ @Service public class StudentServiceImpl implements StudentService { @Autowired StudentDao studentDao; @Override public void addStudent(StudentPO student) { studentDao.addStudent(student); } @Override public void delStudentById(Long id) { studentDao.delStudentById(id); } @Override public void updateStudent(StudentPO student) { studentDao.updateStudent(student); } @Override public List<StudentPO> selectStudent() { return studentDao.selectStudent(); } }
6. 使用Junit5单元测试测试
首先通过IOC容器拿到StudentService对象
private AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class); // 通过Spring的IOC容器 private StudentService studentService = applicationContext.getBean(StudentService.class);
测试
/** * @author YonC * @date 2021/9/2 */ class StudentServiceImplTest { private AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class); // 通过Spring的IOC容器 private StudentService studentService = applicationContext.getBean(StudentService.class); @Test public void testAddStudent() { studentService.addStudent(new StudentPO("zahngsna", "999")); System.out.println("添加成功!"); } @Test public void testDelStudent() { studentService.delStudentById(3L); System.out.println("删除成功!"); } @Test public void testUpdateStudent() { //将id为3的Student的name修改为"wang",age修改为21 studentService.updateStudent(new StudentPO(1L,"wang","28")); System.out.println("修改成功!"); } @Test public void testSelectStudent() { studentService.selectStudent().forEach(System.out::println); } }
到此,关于“怎么用Spring框架+jdbcTemplate实现增删改查功能”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。