为了使JDBC更加易于使用,Spring 在JDBC API上定义了一个抽象层,以此建立一个JDBC存取框架。(便于维护修改)
作为Spring JDBC框架的核心 JDBC模板的设计目的是为不同类型的JDBC操作提供模板方法。每个模板方法都能控制整个给过程,并允许覆盖过程中的特定任务。通过这种方式,可以在尽可能保留灵活性的情况下,将数据库存取的工作量降到最低。
**用JDBC之前需要引入jar包**
我们一般开发用的maven模式,只需要加入Spring Boot JDBC Starter 的代码即可(在maven里面搜索便可以得到)
**jdbc**
普通的JDBC每次使用都需要去链接数据库 关闭数据库 在项目里面很多功能都要去用到数据库,我们便用连接池这种方便的操作。
连接池为应用程序创建固定数量的连接对象,保存在池中进行复用。每次访问时从池中获取已存在,使用完之后,返回池中。
连接池采用了预先建立多个数据库连接对象,然后放到连接池里面去,当客户端请求得时候,取出来一个连接对象为客户端服务,当请求完成可以用.close()将连接对象放回到连接池中去。普通端的到的是物理连接,而在连接池中客户端的得到的是连接对象,一直存在的,大大减少了我们的时间
使用新版的jdbc驱动(cj.jdbc)必须添加时间戳(?serverTimezone=UTC)
我们要用jdbc还是和之前一样,用连接池(方便用不用每次都去创建)
mysql驱动
在maven 里面搜索mysql 就行 , 但是对于我们平常的使用 由于Spring boot里面对这些都有默认的版本值(在pom里面 这些标识版本行会有一个黄色符号),通常可以直接删掉。
db.properties
mysql_driver=com.mysql.cj.jdbc.Driver
mysql_url=jdbc:mysql://localhost:3306/manager?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
mysql_username=root
mysql_passwd=root
在xml配置文件中
<context:property-placeholder location="db.properties"/>
<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>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" value="#{dataSource}"></property>
</bean>
配置完成后在Java类里面便可以使用该连接池
在类里面
@Autowired
private JdbcTemplate jdbcTemplate;
直接就自动装配。
例如
SysUserInfo
@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;
}
UserDao
@Repository
public class UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void insertUserInfo(SysUserInfo su) {
String sql = "insert into 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 getUserById(int userId) {
String sql = "select * from sys_user where user_id=?";
List<SysUserInfo> sl = jdbcTemplate.query(sql, new BeanPropertyRowMapper<SysUserInfo>(SysUserInfo.class),userId);
if(sl != null && sl.size()>0) {
return sl.get(0);
}else
return null;
}
}
applicationContext-jdbc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<context:component-scan base-package="com.zcc.spring.jdbc"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<context:property-placeholder location="db.properties"/>
<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>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" value="#{dataSource}"></property>
</bean>
</beans>
main方法
public class Main {
public static void main(String[] args) {
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext-jdbc.xml");
UserDao ud = ioc.getBean(UserDao.class);
SysUserInfo su = ud.getUserById(1165);
System.out.println(su);
}
}
输入 id便可以查询出来对应的信息。