配置pom文件
<?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>day02_eesy_02account_xmlioc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<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>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>
</project>
文件目录结构
有一个持久层的实体类Dao
有一个service的接口,有一个接口的实现类
实体类代码
package com.itheima.domain;
import java.io.Serializable;
/**
* @Classname Account
* @Description 账户的实体类
* @Date 2020/1/11 20:50
* @Created by mmz
*/
public class Account implements Serializable {
private int id;
private String name;
private float money;
public int getId() {
return id;
}
public void setId(int 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接口
package com.itheima.service;
import com.itheima.domain.Account;
import java.util.List;
/**
* @Classname IAccountService
* @Description 账户的业务层接口
* @Date 2020/1/11 20:49
* @Created by mmz
*/
public interface IAccountService {
//查询索引
List<Account> findAccount();
//查询一个
Account findAccountById(int accoutId);
//保存操作
void saveAccount(Account account);
//更新
void updateAccount(Account account);
//删除
void deleteAccount(int accoutId);
}
service接口的实现类
package com.itheima.service.impl;
import com.itheima.domain.Account;
import com.itheima.service.IAccountService;
import java.util.List;
/**
* @Classname AccoutServiceImpl
* @Description TODO
* @Date 2020/1/11 20:54
* @Created by mmz
*/
public class AccoutServiceImpl implements IAccountService {
public List<Account> findAccount() {
return null;
}
public Account findAccountById(int accoutId) {
return null;
}
public void saveAccount(Account account) {
}
public void updateAccount(Account account) {
}
public void deleteAccount(int accoutId) {
}
}
提示:右键generate implement method 可以引入接口的方法
完成Dao接口的实现类
package com.itheima.dao.impl;
import com.itheima.dao.IAccountDao;
import com.itheima.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.util.List;
/**
* @Classname AccoutDaoImpl
* @Description 账户持久层的实现类
* @Date 2020/1/11 21:42
* @Created by mmz
*/
public class AccoutDaoImpl implements IAccountDao {
private QueryRunner runner;
public QueryRunner getRunner() {
return runner;
}
public void setRunner(QueryRunner runner) {
this.runner = runner;
}
public List<Account> findAllAccount() {
try{
return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
}catch (Exception e){
throw new RuntimeException(e);
}
}
public Account findAccountById(int accoutId) {
try{
return runner.query("select * from account where id = ?",new BeanHandler<Account>(Account.class),accoutId);
}catch (Exception e){
throw new RuntimeException(e);
}
}
public void saveAccount(Account account) {
try{
runner.update("insert into account(name,money) values(?,?)",account.getName(),account.getMoney());
}catch (Exception e){
throw new RuntimeException(e);
}
}
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(e);
}
}
public void deleteAccount(int accoutId) {
try{
runner.update("delete from account where id = ?",accoutId);
}catch (Exception e){
throw new RuntimeException(e);
}
}
}
最主要的配置bean.xml文件
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
配置每一个bean
<bean id="accountService" class="com.itheima.service.impl.AccoutServiceImpl">
<!--注入dao对象-->
<property name="iAccountDao" ref="accoutDao"></property>
</bean>
<bean id="accoutDao" class="com.itheima.dao.impl.AccoutDaoImpl">
<property name="runner" ref="runner"></property>
</bean>
<!--配置QueryRunner对象-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner">
<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/eesy"></property>
<property name="user" value="root"></property>
<property name="password" value="password"></property>
</bean>
利用构造方法配置就是bean中,用到constructor-arg标签,set方法就是property这种标签来定义
运行成功
提示:mysql如果是高版本的,要配置高版本的驱动
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy?serverTimezone=GMT%2B8&characterEncoding=utf-8"></property>
数据库配置的时候要这么写