Spring全家桶系列一一SpringBoot与Mybatis结合

1. mapper.xml、dao接口、实体类自动生成

Spring全家桶一一SpringBoot与Mybatis-LMLPHP

1.1 修改配置文件generator.xml

generator.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <!-- 数据库驱动包位置 也就是刚解压的文件的位置加上 mysql-connector-java-5.1.34.jar-->
    <classPathEntry location="C:\resources\generator\mysql-connector-java-5.1.34.jar" />
    <!--这里也适用Oracle数据库的自动生成-->
    <!-- <classPathEntry location="C:\resources\generator\ojdbc6.jar" /> -->
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 去除生成日期 -->
            <property name="suppressDate" value="true"/>
            <!-- 去除所有的注解 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!-- 数据库链接URL、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/ceilan" userId="root" password="123456">
        <!-- <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="scott" password="tiger">-->
        </jdbcConnection>
        <!--java类型处理器-->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成模型的包名和位置 -->
        <javaModelGenerator targetPackage="com.example.entity" targetProject="C:\resources\generator\src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成的映射文件包名和位置 -->
        <sqlMapGenerator targetPackage="mapping" targetProject="C:\resources\generator\src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.dao" targetProject="C:\resources\generator\src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- 要生成哪个表,更改tableName(数据库里表名)和domainObjectName(实体名,一般首字母大写)就可以 -->
        <table tableName="area" domainObjectName="Area" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
    </context>
</generatorConfiguration>

1.2 用Java运行自动生成

java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite

Spring全家桶一一SpringBoot与Mybatis-LMLPHP

然后把当前目录src下的com文件夹拷贝到项目文件夹下
把mapping文件拷贝到resources文件夹下

Spring全家桶一一SpringBoot与Mybatis-LMLPHP
目录结构如上

2.集成Mybatis框架

2.1 引入依赖

compile "mysql:mysql-connector-java:5.1.39"
compile 'org.mybatis.spring.boot:mybatis-spring-boot-starter:1.2.0'

2.2 启动类DemoApplication.java增加扫描配置

package com.example;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;

@SpringBootApplication
// mapper接口 扫描包配置
@MapperScan(value = "com.example.dao")
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

2.3 添加项目首页index.html

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>Welcome SpringBoot</h1>
</body>
</html>

然后在 DemoApplication 启动类中添加

    //欢迎页面 首页
    @RequestMapping("/")
    public String index(){
        return "index";
    }

之前已经在application.yml中配置了资源映射设置,如下

spring:
  mvc:
    view:
      suffix: .html
  resources:
    static-locations: classpath:/templates

所以现在的项目启动访问 http://localhost:8080/ 是可以直接访问到首页的

3.添加业务层和控制层实现CRUD(增删改查)

增加业务逻辑层包service以及在其下增加impl包用来实现其接口

3.1业务逻辑层接口 AreaService.java

package com.example.service;

import com.example.entity.Area;

/**
 * 这里给dao层的代码拷贝过来先使用
 * created by cfa  2018-11-08 下午 9:56
 **/
public interface AreaService {


    int deleteByPrimaryKey(Integer id);

    int insert(Area record);

    int insertSelective(Area record);

    Area selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(Area record);

    int updateByPrimaryKey(Area record);
}

3.2业务层实现类 AreaServiceImpl.java

package com.example.service.impl;

import com.example.dao.AreaMapper;
import com.example.entity.Area;
import com.example.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;

/**
 * 这里的@Service注解相当于自动注册到Spring的Bean
 * 相当于原来的Application.xml里的 <bean id="areaServiceImpl" class="com.example.service.impl.AreaServiceImpl"/>
 * created by cfa  2018-11-08 下午 9:58
 **/
@Service
public class AreaServiceImpl implements AreaService, Serializable {

    private final AreaMapper areaMapper;

    @Autowired
    public AreaServiceImpl(AreaMapper areaMapper) {
        this.areaMapper = areaMapper;
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return areaMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Area record) {
        return areaMapper.insert(record);
    }

    @Override
    public int insertSelective(Area record) {
        return areaMapper.insertSelective(record);
    }

    @Override
    public Area selectByPrimaryKey(Integer id) {
        return areaMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Area record) {
        return areaMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Area record) {
        return areaMapper.updateByPrimaryKey(record);
    }
}

3.3控制层的AreaController.java

package com.example.controller;


import com.example.entity.Area;
import com.example.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("area")
public class AreaController {

    private final AreaService areaService;

    @Autowired
    public AreaController(AreaService areaService) {
        this.areaService = areaService;
    }

    @RequestMapping("query")
    public Area areaList(){
        return areaService.selectByPrimaryKey(1);
    }

}

看到这里,想必看到很多次@Autowired,@Service等注解了,这就是Spring的两大核心之一的IOC(Inversion of Control),也就是DI依赖注入;
Spring的两大核心AOP和IOC大家面试的时候也基本都有问到,到这里你IOC就不用头疼了;

业务层提供的接口加实现类就是为了实现松耦合,不然一个类就解决了,就像一个手机,坏了,里面的电池,屏幕,主板什么的拆下来还能用,这就是松耦合。

4.热部署插件

5.关于AOP——Spring的又一大核心

5.小叙

01-17 22:20