One,
Spring JDBC is a persistence framework provided by the official spring, and the abstract of jdbc package, eliminating duplicate jdbc repetitive redundant code to make the operation of the database becomes much simpler.
two,
The introduction of the corresponding frame packet
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency>
The introduction of driver
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency>
connection pool
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.21</version> </dependency>
Configure connection pool (special profile)
<context:component-scan base-package="com.zzxtit.spring.jdbc"></context:component-scan>
<context:property-placeholder location="config/DB.properties"/>
Creating DB.properties
mysql_driver=com.mysql.cj.jdbc.Driver mysql_url=jdbc:mysql://locallhost:3306/first?serverTimezone=UTC mysql_username=root mysql_passwd=root
Creating DataSource property value of the injected connection (the difference between el spel expression and expression)
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${mysql_driver}"></property>
<property name="url" value="${mysql_url}"></property>
<property name="username" value="${mysql_username}"></property>
<property name="password" value="${mysql_passwd}"></property>
</bean>
Initialization jdbc
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" value="#{dataSource}"></property>
</bean>
Creating interfaces
package com.zzxtit.spring.jdbc; public interface UserDao { public void insertUserInfo(SysUserInfo su); public SysUserInfo getSysUserById(int userId); }
Create an information class
package com.zzxtit.spring.jdbc; import java.util.Date; import lombok.Data; @Data public class SysUserInfo { private Integer userId; private String userName; private String passwd; private String salt; private String realName; private String avatar; private String phone; private String email; private int gender; private int locked; private Date createTime; private Date updateTime;}
Create an implementation class
package com.zzxtit.spring.jdbc; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jndi.JndiTemplate; import org.springframework.stereotype.Repository; @Repository public class UserDaoImpl implements UserDao{ @Autowired private JndiTemplate jdbcTemplate; public void insertUserInfo(SysUserInfo su) { // TODO Auto-generated method stub String sql="insert into t_sys_user (user_name, passwd, salt, real_name, avatar, phone, email, gender, create_time) " + "values (?, ?, ?, ?, ?, ?, ?, ?, now())"; jdbcTemplate.update(sql, su.getUserName(), su.getPasswd(), su.getSalt(), su.getRealName(), su.getAvatar(), su.getPhone(), su.getEmail(), su.getGender()); } public SysUserInfo getSysUserById(int userId) { // TODO Auto-generated method stub String sql = "select * from t_sys_user where user_id = ?"; List<SysUserInfo> suList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<SysUserInfo>(SysUserInfo.class), userId); if(suList != null && suList.size() > 0) { return suList.get(0); }else { return null; } } }
Use the default constructor with no arguments when you create a bean, if you write a constructor have parameters need to add a no-argument constructor manual