Spring框架
只写了查找所有用户信息
一、目录结构
二、引入pom.xml依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>spring_day02_1_lx</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<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>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
</project>
三、创建domain
package com.itheima.domain;
/**
* @author :lijunxuan
* @date :Created in 2019/5/25 17:25
* @description :
* @version: 1.0
*/
public class Account {
private Integer id;
private String name;
private Float money;
public Account() {
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
public Account(Integer id, String name, Float money) {
this.id = id;
this.name = name;
this.money = money;
}
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;
}
}
四、创建dao和service
AccountDao代码:
package com.itheima.dao;
import com.itheima.domain.Account;
import java.util.List;
/**
* @author :lijunxuan
* @date :Created in 2019/5/25 17:24
* @description :
* @version: 1.0
*/
public interface AccountDao {
/**
* 查询所有用户信息
* @return
*/
public List<Account> findAll();
/**
* 通过名字模糊查询用户信息
* @return
*/
public Account findByUsername(Account account);
/**
* 通过ID查询用户信息
* @return
*/
public Account findByID(Integer id);
/**
* 插入用户信息
* @param account
*/
public void Insert(Account account);
/**
* 删除用户信息
* @param id
*/
public void Delete(Integer id);
/**
* 更新用户信息
* @param account
*/
public void Update(Account account);
}
AccountDaoImpl代码:
package com.itheima.dao.Impl;
import com.itheima.dao.AccountDao;
import com.itheima.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
/**
* @author :lijunxuan
* @date :Created in 2019/5/25 17:33
* @description :
* @version: 1.0
*/
public class AccountDaoImpl implements AccountDao {
private QueryRunner queryRunner;
public void setQueryRunner(QueryRunner queryRunner) {
this.queryRunner = queryRunner;
}
@Override
public List<Account> findAll() {
String sql="select * from account ";
System.out.println("sql:"+sql);
try {
return queryRunner.query(sql,new BeanListHandler<Account>(Account.class));
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Override
public Account findByUsername(Account account) {
return null;
}
@Override
public Account findByID(Integer id) {
return null;
}
@Override
public void Insert(Account account) {
}
@Override
public void Delete(Integer id) {
}
@Override
public void Update(Account account) {
}
}
AccountService代码:
package com.itheima.service;
import com.itheima.domain.Account;
import java.util.List;
public interface AccountService {
/**
* 查询所有用户信息
* @return
*/
public List<Account> findAll();
/**
* 通过名字模糊查询用户信息
* @return
*/
public Account findByUsername(Account account);
/**
* 通过ID查询用户信息
* @return
*/
public Account findByID(Integer id);
/**
* 插入用户信息
* @param account
*/
public void Insert(Account account);
/**
* 删除用户信息
* @param id
*/
public void Delete(Integer id);
/**
* 更新用户信息
* @param account
*/
public void Update(Account account);
}
AccountServiceImpl代码:
package com.itheima.service.Impl;
import com.itheima.dao.AccountDao;
import com.itheima.dao.Impl.AccountDaoImpl;
import com.itheima.domain.Account;
import com.itheima.service.AccountService;
import java.util.List;
/**
* @author :lijunxuan
* @date :Created in 2019/5/25 17:33
* @description :
* @version: 1.0
*/
public class AccountServiceImpl implements AccountService {
AccountDao accountDao=new AccountDaoImpl();
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public List<Account> findAll() {
return accountDao.findAll();
}
@Override
public Account findByUsername(Account account) {
return null;
}
@Override
public Account findByID(Integer id) {
return null;
}
@Override
public void Insert(Account account) {
}
@Override
public void Delete(Integer id) {
}
@Override
public void Update(Account account) {
}
}
五、创建ApplicationContext.xml
通过set方法给属性赋值(注意:必须有一个空的构造方法)
代码如下:
<?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="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/web04"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 配置 QueryRunner -->
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>
<bean id="AccountDao" class="com.itheima.dao.Impl.AccountDaoImpl">
<property name="queryRunner" ref="queryRunner"></property>
</bean>
<bean id="AccountService" class="com.itheima.service.Impl.AccountServiceImpl">
<property name="AccountDao" ref="AccountDao"></property>
</bean>
</beans>
六、测试类TestCRUD
package com.itheima;
import com.itheima.domain.Account;
import com.itheima.service.AccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
* @author :lijunxuan
* @date :Created in 2019/5/25 17:36
* @description :
* @version: 1.0
*/
public class TestCRUD {
/**
* 查询所有用户信息
* @param
*/
@Test
public void testfindAll(){
ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml");
AccountService accountService = ac.getBean("AccountService", AccountService.class);
System.out.println("accountService:"+accountService);
List<Account> accountList = accountService.findAll();
System.out.println("accountList:"+accountList);
for (Account account1 : accountList) {
System.out.println(account1);
}
}
}