一、单表操作简单案例
jar包依赖:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
文件结构如下:
Dao层实现类:
public class AccountDaoImpl implements IAccountDao {
private QueryRunner runner;
public void setRunner(QueryRunner runner) {
this.runner = runner;
}
@Override
public List<Account> findAllAccount() {
try {
return runner.query("select * from account", new BeanListHandler<Account>(Account.class));
}catch(Exception e){
throw new RuntimeException();
}
}
@Override
public Account findAccountById(Integer accountId) {
try {
return runner.query("select * from account where id=?", new BeanHandler<Account>(Account.class),accountId);
}catch(Exception e){
throw new RuntimeException();
}
}
@Override
public void saveAccount(Account account) {
try {
runner.update("insert into account(name,money) values(?,?)",account.getName(),account.getMoney() );
}catch(Exception e){
throw new RuntimeException();
}
}
@Override
public void updateAccount(Account account) {
try {
runner.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
}catch(Exception e){
throw new RuntimeException();
}
}
@Override
public void deleteAccount(Integer accountId) {
try {
runner.update("delete form account where id=?",accountId);
}catch(Exception e){
throw new RuntimeException();
}
}
}
service层实现类:
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<!-- 配置service-->
<bean id="accountService" class="com.itheima.service.Impl.AccountServiceImpl">
<!-- 注入Dao-->
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 配置Dao对象-->
<bean id="accountDao" class="com.itheima.Dao.Impl.AccountDaoImpl">
<!-- 注入QueryRunner-->
<property name="runner" ref="runner"></property>
</bean>
<!-- 配置QueryRunner-->
<!-- 使用多例,防止多个Dao使用同一个QueryRunner产生干扰-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<!-- 注入数据源-->
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>
<!-- 配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--注入连接数据库的必备信息-->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/1906"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>
部分测试代码:
二、使用注解配置
文件结构与上述相同
Dao实现类:
service实现类:
配置文件:
与不使用注解相比:
1、约束空间不同
2、service实现类和Dao层实现类使用了注解配置,则对应的bean标签省略,开启了注解扫描。
<?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"
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:component-scan base-package="com.itheima"></context:component-scan>
<!-- 配置QueryRunner-->
<!-- 使用多例,防止多个Dao使用同一个QueryRunner产生干扰-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<!-- 注入数据源-->
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>
<!-- 配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--注入连接数据库的必备信息-->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/1906"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>
三、不使用xml配置文件
配置类中的注解作用(此处只用一个配置类):
文件结构:
此时将配置类一份为二,一个父配置类,一个与数据库相关的配置类
父配置类Springconfiguration:
JdbcConfig类:
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.user}")
private String user;
@Value("${jdbc.password}")
private String password;
@Bean
@Scope(value="prototype")
public QueryRunner createQueryRunner(DataSource dataSource){
return new QueryRunner(dataSource);
}
@Bean(name="dataSource")
public DataSource createDatasource(){
try {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass(driver);
ds.setJdbcUrl(url);
ds.setUser(user);
ds.setPassword(password);
return ds;
}catch(Exception e){
throw new RuntimeException();
}
}
}
配置文件JdbcConfig.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/1906
jdbc.user=root
jdbc.password=root
由于此时已经没有了bean.xml配置文件,所以在获取容器对象时需要使用到AnnotationConfigApplicationContext实现类: