目录

Life in Flow

知不知,尚矣;不知知,病矣。
不知不知,殆矣。

X

Spring整合Mybatis

引入 Spring 前后对比

非 Spring 环境

 11.实体类与表
 2
 32.业务层接口与实现
 4
 53.数据层接口
 6
 74.Mybatis核心配置
 8
 95.Mybatis映射配置
10
116.客户端程序测试功能

Spring 环境

 11.实体类与表
 2
 32.业务层接口与实现(提供数据层接口的注入操作)
 4
 53.数据层接口
 6
 74.Mybatis核心配置(交给spring控制,该文件省略)
 8
 95.Mybatis映射配置
10
116.客户端程序测试功能(使用spring方式获取bean)
12
137.Spring核心配置文件
14
158.Druid数据源的应用(可选)
16
179.Spring整合MyBatis

导入坐标

Spring 坐标,MyBatis 坐标,MySQL 坐标,Druid 坐标

 1<?xml version="1.0" encoding="UTF-8"?>
 2<project xmlns="http://maven.apache.org/POM/4.0.0"
 3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5    <modelVersion>4.0.0</modelVersion>
 6
 7    <groupId>com.itheima</groupId>
 8    <artifactId>Spring_day01_05_综合案例(spring整合mybatis)</artifactId>
 9    <version>1.0-SNAPSHOT</version>
10
11    <dependencies>
12        <dependency>
13        <groupId>org.mybatis</groupId>
14        <artifactId>mybatis</artifactId>
15        <version>3.5.3</version>
16    </dependency>
17        <dependency>
18            <groupId>mysql</groupId>
19            <artifactId>mysql-connector-java</artifactId>
20            <version>5.1.47</version>
21        </dependency>
22        <dependency>
23            <groupId>org.springframework</groupId>
24            <artifactId>spring-context</artifactId>
25            <version>5.1.9.RELEASE</version>
26        </dependency>
27        <dependency>
28            <groupId>org.springframework</groupId>
29            <artifactId>spring-jdbc</artifactId>
30            <version>5.1.9.RELEASE</version>
31        </dependency>
32        <dependency>
33            <groupId>com.alibaba</groupId>
34            <artifactId>druid</artifactId>
35            <version>1.1.16</version>
36        </dependency>
37        <dependency>
38            <groupId>org.mybatis</groupId>
39            <artifactId>mybatis-spring</artifactId>
40            <version>1.3.0</version>
41        </dependency>
42    </dependencies>
43
44</project>

创建数据库表

1CREATE TABLE account(id INT(20) PRIMARY KEY NOT NULL auto_increment, name varchar(16), money DOUBLE);
2INSERT INTO account (name,money) VALUES ("Mike",888.88)
3INSERT INTO account (name,money) VALUES  ("Jock",666.66)

Domain、DAO、Service

Domain

 1package com.itheima.domain;
 2
 3import java.io.Serializable;
 4
 5public class Account implements Serializable {
 6
 7    private Integer id;
 8    private String name;
 9    private Double money;
10
11    public Integer getId() {
12        return id;
13    }
14
15    public void setId(Integer id) {
16        this.id = id;
17    }
18
19    public String getName() {
20        return name;
21    }
22
23    public void setName(String name) {
24        this.name = name;
25    }
26
27    public Double getMoney() {
28        return money;
29    }
30
31    public void setMoney(Double money) {
32        this.money = money;
33    }
34
35    @Override
36    public String toString() {
37        return "Account{" +
38                "id=" + id +
39                ", name='" + name + '\'' +
40                ", money=" + money +
41                '}';
42    }
43}
44

DAO

 1package com.itheima.dao;
 2
 3import com.itheima.domain.Account;
 4
 5import java.util.List;
 6
 7public interface AccountDao {
 8
 9    void save(Account account);
10
11    void delete(Integer id);
12
13    void update(Account account);
14
15    List<Account> findAll();
16
17    Account findById(Integer id);
18}
19

Service

1interface
 1package com.itheima.service;
 2
 3import com.itheima.domain.Account;
 4
 5import java.util.List;
 6
 7public interface AccountService {
 8
 9    void save(Account account);
10
11    void delete(Integer id);
12
13    void update(Account account);
14
15    List<Account> findAll();
16
17    Account findById(Integer id);
18
19}
20
1impl
 1package com.itheima.service.impl;
 2
 3
 4import com.itheima.dao.AccountDao;
 5import com.itheima.domain.Account;
 6import com.itheima.service.AccountService;
 7
 8import java.util.List;
 9
10public class AccountServiceImpl implements AccountService {
11
12    private AccountDao accountDao;
13
14    public void setAccountDao(AccountDao accountDao) {
15        this.accountDao = accountDao;
16    }
17
18    public void save(Account account) {
19        accountDao.save(account);
20    }
21
22    public void update(Account account){
23        accountDao.update(account);
24    }
25
26    public void delete(Integer id) {
27        accountDao.delete(id);
28    }
29
30    public Account findById(Integer id) {
31        return accountDao.findById(id);
32    }
33
34    public List<Account> findAll() {
35        return accountDao.findAll();
36    }
37}
38

Mapper.xml

src/main/resources/com/itheima/dao/AccountDao.xml

 1<?xml version="1.0" encoding="UTF-8"?>
 2<!DOCTYPE mapper
 3        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5<mapper namespace="com.itheima.dao.AccountDao">
 6
 7    <!--配置根据id查询-->
 8    <select id="findById" resultType="account" parameterType="int">
 9        select * from account where id = #{id}
10    </select>
11
12    <!--配置查询所有-->
13    <select id="findAll" resultType="account">
14        select * from account
15    </select>
16
17    <!--配置保存-->
18    <insert id="save" parameterType="account">
19        insert into account(name,money)values(#{name},#{money})
20    </insert>
21
22    <!--配置删除-->
23    <delete id="delete" parameterType="int">
24        delete from account where id = #{id}
25    </delete>
26
27    <!--配置更新-->
28    <update id="update" parameterType="account">
29        update account set name=#{name},money=#{money} where id=#{id}
30    </update>
31</mapper>

jdbc.properties

src/main/resources/jdbc.properties

1jdbc.driver=com.mysql.jdbc.Driver
2jdbc.url=jdbc:mysql://192.168.31.101:50000/spring_db
3jdbc.username=root
4jdbc.password=123456

applicationContext.xml

src/main/resources/applicationContext.xml

 1<?xml version="1.0" encoding="UTF-8"?>
 2<beans xmlns="http://www.springframework.org/schema/beans"
 3       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4       xmlns:context="http://www.springframework.org/schema/context"
 5       xsi:schemaLocation="http://www.springframework.org/schema/beans
 6        https://www.springframework.org/schema/beans/spring-beans.xsd
 7        http://www.springframework.org/schema/context
 8        https://www.springframework.org/schema/context/spring-context.xsd">
 9
10    <!--加载perperties配置文件的信息-->
11    <context:property-placeholder location="classpath:*.properties"/>
12
13    <!--加载druid资源-->
14    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
15        <property name="driverClassName" value="${jdbc.driver}"/>
16        <property name="url" value="${jdbc.url}"/>
17        <property name="username" value="${jdbc.username}"/>
18        <property name="password" value="${jdbc.password}"/>
19    </bean>
20
21    <!--配置service作为spring的bean,并注入dao-->
22    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
23        <property name="accountDao" ref="accountDao"/>
24    </bean>
25
26    <!--spring整合mybatis后控制的创建连接SqlSession用的对象-->
27    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
28        <property name="dataSource" ref="dataSource"/>
29        <!-- domain别名配置,避免写全路径 -->
30        <property name="typeAliasesPackage" value="com.itheima.domain"/>
31    </bean>
32
33    <!--加载mybatis映射配置的扫描,将其作为spring的bean进行管理-->
34    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
35        <property name="basePackage" value="com.itheima.dao"/>
36    </bean>
37
38
39
40</beans>

测试

 1import com.itheima.domain.Account;
 2import com.itheima.service.AccountService;
 3import org.springframework.context.ApplicationContext;
 4import org.springframework.context.support.ClassPathXmlApplicationContext;
 5
 6public class App {
 7    public static void main(String[] args) {
 8        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
 9        AccountService accountService = (AccountService) ctx.getBean("accountService");
10        Account ac = accountService.findById(2);
11        System.out.println(ac);
12
13//        Account account = new Account();
14//        account.setName("Tom");
15//        account.setMoney(123456.78);
16//
17//        accountService.save(account);
18
19    }
20}
21

作者:Soulboy