Spring encapsulates JDBC, known as the JdbcTemplate, to make working with databases easier.
First, preparation
1. Introduce dependencies
Build on the previous ones and introduce these dependencies.
2. Configure the database connection pool in the configuration file
External file jdbc.properties:
prop.driverClass=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/userDb
prop.username=root
prop.password=123456
Copy the code
Configuration file import:
<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="com.pingguo.spring5"></context:component-scan> <! - the introduction of external properties file - > < context: the property - placeholder location = "classpath: JDBC. Properties" / > <! - configure the connection pool - > < bean id = "dataSource" class = "com. Alibaba. Druid. Pool. DruidDataSource" > < property name = "driverClassName." value="${prop.driverClass}"></property> <property name="url" value="${prop.url}"></property> <property name="username" value="${prop.username}"></property> <property name="password" value="${prop.password}"></property> </bean> </beans>Copy the code
3. Configure JdbcTemplate
Inject the DataSource.
. . <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <! < datasource name=" datasource "></property> </bean>... .Copy the code
4. Inject the JdbcTemplate object into the DAO
Create a DAO to inject the JdbcTemplate into.
@repository public class BookDaoImpl implements BookDao {// Autowired private jdbcTemplate jdbcTemplate; }Copy the code
Create the Service class to inject the DAO into.
@service public class BookService {@autowired private BookDao BookDao; }Copy the code
Two, operation database
Take add as an example.
Create a very simple table with three fields.
1. Create an entity class
Create the entity class corresponding to the data table, and generate get and set methods for the three attributes.
public class Book { private String userId; private String username; private String userStatus; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; }... .Copy the code
2. Write services and DAOs
service
@service public class BookService {@autowired private BookDao BookDao; public void addBook(Book book) { bookDao.add(book); }}Copy the code
The dao implementation class.
@repository public class BookDaoImpl implements BookDao {// Autowired private jdbcTemplate jdbcTemplate; @Override public void add(Book book) { String sql = "insert into t_book values (? ,? ,?) "; int result = jdbcTemplate.update(sql, book.getUserId(), book.getUsername(), book.getUserStatus()); System.out.println(result); }}Copy the code
Use the jdbctemplate.update () method to add, the first parameter is SQL, the second variable length parameter, and return 1 on success.
3. Write tests
public class TestBook { @Test public void testJdbc() { ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); BookService bookService = context.getBean("bookService", BookService.class); Book book = new Book(); book.setUserId("1"); book.setUsername("ceshi"); book.setUserStatus("3"); bookService.addBook(book); }}Copy the code
Running results:
August 5, 2021 10:35:15 afternoon com. Alibaba. Druid. Pool. DruidDataSource info information: {dataSource-1} inited 1 Process finished with exit code 0Copy the code
View tables
Succeeded in adding.
Delete and modify operations similar to the above, not demo.