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

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

SpringBoot集成MyBatis

項目名稱:010-springboot-web-mybatis

案例思路

通過SpringBoot +MyBatis實現對數據庫學生表的查詢操作;

數據庫參考:springboot.sql腳本文件。

實現步驟

1.準備數據庫

啟動Linux系統上的mySQL服務器,通過Navicat連接

創建新的數據庫springboot,指定數據庫字符編碼為utf-8

向表中插入數據

2.創建010-springboot-web-mybatis項目

創建一個新的SpringBoot的Module

指定GAV坐標

選擇SpringBoot版本以及web依賴

修改Content root以及Mudule file location

3.在pom.xml中添加相關jar依賴

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

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

4.在Springboot的核心配置文件application.properties中配置數據源

注意根據自己數據庫的信息修改以下內容

#配置內嵌Tomcat端口號
server.port=9090

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

#配置數據庫的連接信息
#注意這里的驅動類有變化
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.開發代碼

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

在com.bjpowernode.springboot.web包下創建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包下創建service接口并編寫代碼

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

 */
public interface StudentService {

    /**
     * 根據學生標識獲取學生詳情
     * @param id
     * @return
     */
    Student queryStudentById(Integer id);
}

在com.bjpowernode.springboot.service.impl包下創建service接口并編寫代碼

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

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

如果在web中導入service存在報錯,可以嘗試進行如下配置解決

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

@Mapper作用:mybatis自動掃描數據持久層的映射文件及DAO接口的關系

@Mapper
public interface StudentMapper {

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

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

6.啟動Application應用,瀏覽器訪問測試運行

DAO其它開發方式

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

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

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

測試運行

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

項目名稱:011-springboot-web-mybatis

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

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

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

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

 

全部教程
主站蜘蛛池模板: 国产成人综合自拍 | 中文字幕欧美日韩在线不卡 | 亚洲精品αv一区二区三区 亚洲精品爱爱久久 | 在线亚洲 欧美 日本专区 | 激情在线播放免费视频高清 | 成人免费高清视频 | 欧美一级毛片一免费 | 女人隐私秘视频黄www免费 | 高清影院|精品秒播3 | 高清成人 | 久久精品亚洲欧美va | 草久在线观看视频 | 91新网站| 看一级特黄a大片日本片黑人 | 欧美aⅴ | xxx色视频| 欧美成人aa大片拍拍拍 | 99久久精品视香蕉蕉er热资源 | 欧美成人se01短视频在线看 | 日日干综合 | 国产精品视频不卡 | 成 人国产在线观看高清不卡 | 国产精品成人免费视频 | 日韩射吧| 爱爱永久免费视频网站 | 日本不卡视频网站 | 亚洲综合第一欧美日韩中文 | 天天插日日操 | 狠狠色噜噜狠狠色综合久 | 国产精品99爱免费视频 | 在线观看一级毛片免费 | 免费福利入口在线观看 | 高清不卡日本v在线二区 | 强制高潮18xxxxhd日韩 | 精品久久久久久久一区二区手机版 | 久久久青草 | 国产高清国内精品福利色噜噜 | 五月天婷婷在线观看高清 | 久久精品国产清自在天天线 | 日日摸夜夜摸无需播放器 | 就要爱综合|