大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

SpringBoot教程
SpringBoot入門案例
SpringBoot框架Web開發(fā)
SpringBoot非web應(yīng)用程序
SpringBoot使用攔截器
SpringBoot中使用Servlet
SpringBoot中使用Filter
SpringBoot項(xiàng)目配置字符編碼
SpringBoot打包與部署
SpringBoot使用Actuator
SpringBoot集成Thymeleaf模板
SpringBoot總結(jié)及綜合案例
SpringBoot工程下使用Mybatis反向工程

SpringBoot集成MyBatis

項(xiàng)目名稱:010-springboot-web-mybatis

案例思路

通過SpringBoot +MyBatis實(shí)現(xiàn)對數(shù)據(jù)庫學(xué)生表的查詢操作;

數(shù)據(jù)庫參考:springboot.sql腳本文件。

實(shí)現(xiàn)步驟

1.準(zhǔn)備數(shù)據(jù)庫

啟動Linux系統(tǒng)上的mySQL服務(wù)器,通過Navicat連接

創(chuàng)建新的數(shù)據(jù)庫springboot,指定數(shù)據(jù)庫字符編碼為utf-8

向表中插入數(shù)據(jù)

2.創(chuàng)建010-springboot-web-mybatis項(xiàng)目

創(chuàng)建一個(gè)新的SpringBoot的Module

指定GAV坐標(biāo)

選擇SpringBoot版本以及web依賴

修改Content root以及Mudule file location

3.在pom.xml中添加相關(guān)jar依賴

<!--MyBatis整合SpringBoot的起步依賴-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.0</version>
</dependency>

<!--MySQL的驅(qū)動依賴-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

4.在Springboot的核心配置文件application.properties中配置數(shù)據(jù)源

注意根據(jù)自己數(shù)據(jù)庫的信息修改以下內(nèi)容

#配置內(nèi)嵌Tomcat端口號
server.port=9090

#配置項(xiàng)目上下文根
server.servlet.context-path=/010-springboot-web-mybatis

#配置數(shù)據(jù)庫的連接信息
#注意這里的驅(qū)動類有變化
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.92.134:3306/springboot?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456

5.開發(fā)代碼

使用Mybatis反向工程生成接口、映射文件以及實(shí)體bean,具體步驟參見附錄1

在com.bjpowernode.springboot.web包下創(chuàng)建StudentController并編寫代碼

/**
 * ClassName:StudentController
 * Package:com.bjpowernode.springboot.web
 * Description:

 */
@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/springBoot/student")
    public @ResponseBody Object student() {

        Student student = studentService.queryStudentById(1);

        return student;
    }
}

在com.bjpowernode.springboot.service包下創(chuàng)建service接口并編寫代碼

/**
 * ClassName:StudentService
 * Package:com.bjpowernode.springboot.service
 * Description:

 */
public interface StudentService {

    /**
     * 根據(jù)學(xué)生標(biāo)識獲取學(xué)生詳情
     * @param id
     * @return
     */
    Student queryStudentById(Integer id);
}

在com.bjpowernode.springboot.service.impl包下創(chuàng)建service接口并編寫代碼

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Student queryStudentById(Integer id) {
        return studentMapper.selectByPrimaryKey(id);
    }
}

如果在web中導(dǎo)入service存在報(bào)錯,可以嘗試進(jìn)行如下配置解決

在Mybatis反向工程生成的StudentMapper接口上加一個(gè)Mapper注解

@Mapper作用:mybatis自動掃描數(shù)據(jù)持久層的映射文件及DAO接口的關(guān)系

@Mapper
public interface StudentMapper {

注意:默認(rèn)情況下,Mybatis的xml映射文件不會編譯到target的class目錄下,所以我們需要在pom.xml文件中配置resource

<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.xml</include>
        </includes>
    </resource>
</resources>

6.啟動Application應(yīng)用,瀏覽器訪問測試運(yùn)行

DAO其它開發(fā)方式

1.在運(yùn)行的主類上添加注解包掃描@MapperScan("com.bjpowernode.springboot.mapper")

注釋掉StudentMapper接口上的@Mapper注解

在運(yùn)行主類Application上加@MapperScan("com.bjpowernode.springboot.mapper")

測試運(yùn)行

2.將接口和映射文件分開

項(xiàng)目名稱:011-springboot-web-mybatis

因?yàn)镾pringBoot不能自動編譯接口映射的xml文件,還需要手動在pom文件中指定,所以有的公司直接將映射文件直接放到resources目錄下;

在resources目錄下新建目錄mapper存放映射文件,將StudentMapper.xml文件移到resources/mapper目錄下;

在application.properties配置文件中指定映射文件的位置,這個(gè)配置只有接口和映射文件不在同一個(gè)包的情況下,才需要指定

# 指定Mybatis映射文件的路徑
mybatis.mapper-locations=classpath:mapper/*.xml

 

全部教程
主站蜘蛛池模板: 99热久久国产精品这里有99 | 国内精品51视频在线观看 | 日韩一级大片 | 神马影院我不卡在线观看 | 亚洲在线中文 | 越猛烈欧美xx00动态图免费 | 妇女bbw奶水bbw | 精品日本久久久久久久久久 | 在线中文字幕观看 | 欧美黄色毛片 | 99久久精品国产一区二区成人 | 香香影院在线观看 | 欧美人与动性xxxxbbbb | 九九在线免费观看视频 | 午夜影院操一 | 亚洲乱码一区二区三区在线观看 | 女性下面全部视频免费 | 94在线| 开心久久婷婷综合中文字幕 | 日本综合视频 | 亚洲小视频在线 | 麻豆视频一区二区 | 日日日日人人人夜夜夜2017 | 久久久无码精品亚洲日韩按摩 | 亚洲欧美日韩中文字幕在线一区 | 欧美一区二区在线观看免费网站 | 深夜在线看| 国产爱| 欧美午夜精品久久久久免费视 | 国产成+人欧美+综合在线观看 | 色在线视频观看 | 欧美午夜精品久久久久免费视 | 天天做天天爱夜夜大爽完整 | 精品哟啊呦v视频在线观看 精品哟哟国产在线观看 | 国内成人精品视频 | 九九夜色 | 在线观看精品一区 | 国产在线播放91 | 国产一级视频在线观看 | 亚洲精品成人7777在线观看 | 日韩天天干 |