项目结构
![](https://i-blog.csdnimg.cn/blog_migrate/89f6fa37f971926471805875841230d8.png)
com.zyj.dao.AccountDao
package com.zyj.dao;
import com.zyj.entity.Account;
import java.util.List;
public interface AccountDao {
public abstract void save(Account account);
public abstract void delete(Integer id);
public abstract void update(Account account);
public abstract List<Account> findAll();
public abstract Account findById(Integer id);
}
com.zyj.entity.Account
package com.zyj.entity;
public class Account {
private Integer id;
private String name;
private Double money;
public Account() {
}
public Account(Integer id, String name, Double 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 Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
com.zyj.service.Impl.AccountServiceImpl
package com.zyj.service.Impl;
import com.zyj.dao.AccountDao;
import com.zyj.entity.Account;
import com.zyj.service.AccountService;
import java.util.List;
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void save(Account account) {
accountDao.save(account);
}
@Override
public void update(Account account) {
accountDao.update(account);
}
@Override
public void delete(Integer id) {
accountDao.delete(id);
}
@Override
public Account findById(Integer id) {
return accountDao.findById(id);
}
@Override
public List<Account> findAll() {
return accountDao.findAll();
}
}
com.zyj.service.AccountService
package com.zyj.service;
import com.zyj.entity.Account;
import java.util.List;
public interface AccountService {
public void save(Account account);
public void update(Account account);
public void delete(Integer id);
public Account findById(Integer id);
public List<Account> findAll();
}
APP
import com.zyj.entity.Account;
import com.zyj.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class APP {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = (AccountService) ctx.getBean("accountService");
//以查询全部表数据为测试用例
List<Account> all = accountService.findAll();
System.out.println(all);
}
}
src/main/resources/mapper/AccountDao.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zyj.dao.AccountDao">
<!--配置根据id查询-->
<select id="findById" resultType="com.zyj.entity.Account" parameterType="int">
select *
from account
where id=#{id}
</select>
<!--配置查询所有-->
<select id="findAll" resultType="com.zyj.entity.Account">
select *
from account
</select>
<!--配置保存-->
<select id="save" parameterType="com.zyj.entity.Account">
insert into account(name,money)
values(#{name},#{money})
</select>
<!--配置删除-->
<select id="delete" parameterType="int">
delete from account
where id=#{id}
</select>
<!--配置更新-->
<select id="update" parameterType="com.zyj.entity.Account">
update account
set name=#{name},money=#{money}
where id=#{id}
</select>
</mapper>
src/main/resources/applicationContext.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
">
<!--加载properties配置文件的信息-->
<context:property-placeholder location="classpath*:*.properties"/>
<!--加载Druid资源,并配置参数-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialSize" value="${jdbc.initialSize}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
<property name="minIdle" value="${jdbc.minIdle}"/>
<property name="maxWait" value="${jdbc.maxWait}"/>
</bean>
<!--配置service作为spring的bean,注入dao-->
<bean id="accountService" class="com.zyj.service.Impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!--spring整合mybatis后控制的创建连接用的对象-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.zyj.entity"/>
<property name="mapperLocations" value="classpath*:mapper/*.xml"/>
</bean>
<!--扫描所有的dao接口, 加载mybatis映射配置的扫描,将其作为spring的bean进行管理-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zyj.dao"/>
</bean>
</beans>
src/main/resources/jdbc.properties
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.168.1.199:1521:orcl
jdbc.username=zyj
jdbc.password=oracle
jdbc.initialSize=30
jdbc.maxActive=100
jdbc.minIdle=30
jdbc.maxWait=10000
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.zyj</groupId>
<artifactId>Spring_day01_05_spring整合mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<!--oracle-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>
<!--mybatis_-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<!--spring整合mybatis的jar包,归属于mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
</project>