首先创建一个maven框架
我们来给pom.xml来增加一些依赖
//首先增加上一个packaging
<packaging>jar</packaging>
//然后添加依赖,包括spring框架的、用于数据库的、连接池坐标的及junit测试的。
<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>
然后我们来创建一个service包
先创建一个业务层的接口
//在接口内定义一个方法
List<Account> FindAll();
然后创建一个impl包,其中建立service的实现类
//针对于声明的accountDao对象,使用set方法
private IAccountDao accountDao;
public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
}
public List<Account> FindAll(){
return accountDao.FindAll();
}
我们再来创建此时还在报红的Account类
类名那个地方要加implements Serializable,才可以使用。
//我们建立一个Account类,可以新建一个包,将其放于包下
private Integer id;
private String name;
private float money;
//建立这些变量的get和set方法,并建立toString()方法
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
然后再来创建service实现类中使用的dao对象
先创建dao包,在dao包下建立IAccountDao这个接口
在创建一个impl包,里面建立dao的实现类AccountDaoImpl
private QueryRunner runner;
public void setRunner(QueryRunner runner) {
this.runner = runner;
}
public List<Account> FindAll(){
try{
return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
}catch(Exception e){
throw new RuntimeException(e);
}
}
我们来建立bean.xml来使得所设的对象彼此能够形成联系
//首先给它增加xml的约束
<?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">
//然后来添加相关的bean标签(这里的id与class都是根据自己的程序来的)
<bean id="accountService" class="goulizi.service.impl.AccountServiceImpl">
//因为在AccountServiceImpl实现类中,我们定义了accountDao对象,且使用了set方法,因此这里要对他进行注入
//且这里ref的使用是由下面那个bean所设定的,下面的亦是同理
<property name="accountDao" ref="accountDao"></property>
</bean>
<bean id="accountDao" class="goulizi.dao.impl.AccountDaoImpl">
<property name="runner" ref="runner"></property>
</bean>
<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/test"></property>
<property name="user" value="root"></property>
<property name="password" value="1234"></property>
</bean>
</beans>
最后来创建测试类
//编写测试方法
@Test
public void TestFindAll(){
//1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.得到业务层对象
IAccountService as = ac.getBean("accountService",IAccountService.class);
//3.执行方法
List<Account> accounts = as.findAllAccount();
for (Account account : accounts){
System.out.println(account);
}
}
然后就可以运行了,如下截图:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIwczX0xiRGZkRGZ0Xy9GbvNGL2EzXlpXazxSerpnTzElaOFTU6hFeGNDTwYVbiVHNHpleO1GTulzRilWO5xkNNh0YwIFSh9Fd4VGdsATMfd3bkFGazxyaHRGcWdUYuVzVa9GczoVdG1mWfVGc5RHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-cmbw5CN0MTN1QTM3IjMwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)