Spring JDBC框架
Spring JDBC简介
Spring JDBC 是spring 官方提供的对jdbc进行了抽象和封装使操作数据库的一个包。Spring框架针对数据库提供了JdbcTemplate 类,JdbcTemplate 是Spring 数据抽象层的基础,其他更高层次的抽象类都是构建在其基础之上。
Spring JDBC核心
Spring JDBC的核心是JdbcTemplate模板类,通过该对象可以对数据库操作进行封装,提供一种对数据库进行简化操作的API。它继承自抽象类JdbcAccessor,同时实现了JdbcOperations 接口,可以使用增删改查来对数据库进行操作。JdbcTemplate 的直接父类JdbcAccessor提供了一些访问数据库时所需要使用的公共属性,包括DataSource以及SQLExceptionTranslator 等
JdbcTemplate主要提供以下方法操作数据库表中的数据:
- execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句;
- update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句;
- query方法及queryForXXX方法:用于执行查询相关语句;
- call方法:用于执行存储过程、函数相关语句。
Spring JDBC开发过程
1、新建 maven 项目
2、引入Spring相关包
<dependencies>
<!-- spring对Bean管理的包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- spring 对容器管理的包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- spring jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<!-- mysql包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
</dependencies>
3、创建DAO层接口和实现类。
public interface UserDao {
int saveUser(User user);
List<User> findAll();
int updateUser(User user);
}
4、创建DAO接口的实现类
JdbcDaoSupport 类是Spring JDBC提供的对DAO层的支持,主要封装了jdbcTemplate类 ,所有的SQL语句可以通过 jdbcTemplate发出。
public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
private JdbcTemplate jdbcTemplate;
@Override
public int saveUser(User user) {
final String sql = "insert into user values (null,?,?,?,?)";
return getJdbcTemplate()
.update(sql,user.getAcctName(),user.getPassword()
,user.getName(),user.getJob());
}
@Override
public List<User> findAll() {
final String sql ="select * from user";
List<User> userlist = getJdbcTemplate().query(sql, new RowMapper<User>() {
@Override
public User mapRow(ResultSet resultSet, int i) throws SQLException {
User user = new User();
user.setUserId(resultSet.getInt(1));
user.setAcctName(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
user.setName(resultSet.getString(4));
user.setJob(resultSet.getString(5));
return user;
}
});
return userlist;
}
@Override
public int updateUser(User user) {
return getJdbcTemplate().update("update user set acctName=?,password=?,name=?,job=? where userId=?"
,user.getAcctName(),user.getPassword(),user.getName()
,user.getUserId(),user.getJob());
}
}
创建数据源属性文件jdbc.properties属性文件
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/wang
jdbc.username=root
jdbc.password=root
创建Spring核心配置文件applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<!-- 加载数据源的配置-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 第三步 userDao配置到springIOC容器中-->
<bean id="userDao" class="org.example.dao.impl.UserDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!--第一步 数据源负责连接数据库,交给ioc管理-->
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--第二步 将jdbc template模板类交给spring容器管理-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="datasource"/>
</bean>
<!--第四步 userService业务层注入-->
<bean id="userService" class="org.example.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
</beans>
在test文件夹下创建测试
public class AppTest
{
@Test
public void testQueryAllUsers(){
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) applicationContext.getBean("userService");
List<User> userList = userService.findAllUsers();
for (User user:userList){
System.out.println(user);
}
}
@Test
public void testSaveUser(){
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) applicationContext.getBean("userService");
int i = userService.registerUserInfo("ff","11","1111");
System.out.println("success");
}
}
存在的问题:
由上面的程序我们可以看出,每个测试方法都重新获取了一次 spring 的核心容器,造成了不必要的重复代码,增加了开发的工作量。所以应该避免因此可以利用注解的ioc配置把容器的获取定义到类中去。
基于注解的 IOC 配置
其实注解配置和 xml 配置要实现的功能相同只是形式不同,目的都是降低程序间的耦合。
环境搭建
1、添加AOP包支持
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
注:其它的包正常添加
注解实现对象注入
1、创建对象的注解
-
@Component
-
作用:把资源让 spring 来管理。相当于在 xml 中配置一个 bean
属性:
value:指定 bean 的 id。如果不指定 value 属性,默认 bean 的 id 是当前类的类名。首字母小写
相当于:
有四种注解都充当bean的标签,以下三个有一定的业务含义:
- @Controller 注解用来注解WEB层的类,例如:spring mvc -> controller类
- @Service :注解业务层的类, 例如:ServiceImpl实现类
- @Repository:一般用于对持久层DAO的实现类进行注解。
注意:
用来替换bean标签的注解写在类的定义(类名)上面,不能写在类的代码中
如果注解中有且只有一个属性要赋值时,且名称是 value,value 在赋值可以不写。
@Service("userService")
2、用于注入数据的注解标签
相当于:
< property name="" ref="">
< property name="" value="">
- @Autowired
- 作用:自动按类型注入,set 方法可以省略。只能注入其他 bean 类型,当有多个类型匹配时,使用要注入的对象变量名称作为 bean 的 id,在 spring 容器查找,找到了也可以注入成功。找不到就报错。该注解写在属性的上面。
@Autowired
private UserDao userDao;
- @Qualifier
- 作用:在自动按类型注入的基础上再按照Bean 的 id 注入。它给字段注入时不能独立使用,必须和@Autowired 一起使用;但是给方法参数注入时,可以独立使用。
属性value:指定 bean 的 id。
@Qualifier("userDao")
- @Resource
- 作用:用于注入数据,按照 Bean 的 id 注入。它也只能注入其他 bean 类型。
- 属性name:指定 bean 的 id。
@Resource(name = "acctDao")
private AcctDao acctDao;
- @Value
- 作用:注入基本数据类型和 String 类型数据的属性:
value:用于指定值
@Value("${username}")
private String username;
@Value("1111")
private String password;
- 修改ApplicationContext.xml配置:
<context:component-scan base-package="com.huikedu"></context:component-scan>
spring 容器扫描所指定的包,com.huikedu包所有类和子包下的所有含有 @respositroy,@Service,@Component,@controller的注解扫描至Spring ioc的容器中。
小结:
Spring注解和XML配置选择问题:
-
注解的优势:配置简单,维护方便(我们找到类,就相当于找到了对应的配置)。
-
XML 的优势:修改时,不用改源码。不涉及重新编译和部署。
类的注解配置
@Configuration注解
- 作用:用于指定当前类是一个 spring 配置类,当创建容器时会从该类上加载注解。获取容器时需要使用AnnotationApplicationContext(有@Configuration 注解的类.class)。
属性value:用于指定配置类的字节码
示例代码:
@Configuration
public class SpringConfiguration {
}
@ComponentScan注解
- 作用:用于指定 spring 在初始化容器时要扫描的包。
- 属性basePackages:用于指定要扫描的包。和该注解中的 value 属性作用一样。
@Configuration
/*
相当于
<context:component-scan base-package="org.example"></context:component-scan>
*/
@ComponentScan("org.example")
public class SpringConfiguration {
}
@Bean注解
- 作用:只能写在方法上,表明使用此方法创建一个对象,并且放入 spring 容器
- 属性name:给当前@Bean注解方法创建的对象指定一个名称(即 bean 的 id)。
@Bean(name = "dataSource")
public DataSource dataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/wang");
dataSource.setUser("root");
dataSource.setPassword("root");
return dataSource;
}
@PropertySource注解
- 作用: 加载.properties 文件中的配置。
属性value[]:用于指定 properties 文件位置。如果是在类路径下,需要写上 classpath:
配置数据源:
public class JdbcConfig {
//创建一个数据源,存入 spring 容器中
@Value("${jdbc.driverClass}")
private String driverClass;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean(name = "ds")
public DataSource crdataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(driverClass);
dataSource.setJdbcUrl(url);
dataSource.setUser(username);
dataSource.setPassword(password);
return dataSource;
}
jdbc.properties 文件:
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/wang
jdbc.username=root
jdbc.password=root
@Import 注解
- 作用:导入其他配置类,引入其他配置类时,可以不写@Configuration 注解。
属性value[]:用于指定其他配置类的字节码。
//主配置
@Configuration
//导入其他分支配置 class类的配置分支
@Import({jdbcConfigtation.class})
@ComponentScan("org.example")
@PropertySource(value = "jdbc1.properties")
public class SpringConfiguration {
}
RunWith注解
- 作用:让测试运行于Spring测试环境
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringConfiguration.class})
public class AppTest {
@Autowired
UserService userService;
@Test
public void testQueryUserInfo(){
List<User> userList = userService.findAllUsers();
for (User user:userList){
System.out.println(user);
}
}
}