1. 程式人生 > >springboot和mybatis整合(單多表)

springboot和mybatis整合(單多表)

1、MyBatis介紹

MyBatis 本是apache的一個開源專案iBatis, 2010年這個專案由apache software foundation 遷移到了google code,並且改名為MyBatis,實質上Mybatisibatis進行一些改進。

MyBatis是一個優秀的持久層框架,它對jdbc的操作資料庫的過程進行封裝,使開發者只需要關注 SQL 本身,而不需要花費精力去處理例如註冊驅動、建立connection、建立statement、手動設定引數、結果集檢索等jdbc繁雜的過程程式碼。

Mybatis通過xml或註解的方式將要執行的各種statementstatement

preparedStatemntCallableStatement)配置起來,並通過java物件和statement中的sql進行對映生成最終執行的sql語句,最後由mybatis框架執行sql並將結果對映成java物件並返回。

 

mybatis初期使用比較麻煩,需要各種配置檔案、實體類、dao層對映關聯、還有一大推其它配置。當然mybatis也發現了這種弊端,初期開發了generator可以根據表結果自動生產實體類、配置檔案和dao層程式碼,可以減輕一部分開發量;後期也進行了大量的優化可以使用註解了,自動管理dao層和配置檔案等,發展到最頂端就是今天要講的這種模式了,mybatis-spring-boot-starter

就是springboot+mybatis可以完全註解不用配置檔案,也可以簡單配置輕鬆上手。

2、整合

1)在pom.xml里加入以下程式碼

<dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.3.1</version>
</

dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <scope>runtime</scope>
</dependency>

會自動下載相關的包

 

2)application.properties 新增相關配置

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url
=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username
=root
spring.datasource.password
=root

mybatis.mapper-locations
=classpath:/mapper/*Mapper.xml
mybatis.type-aliases-package
=com.twj.login

 

3、建立資料庫

建立user表(直接用上次的)

 

編寫實體類

4、註冊

@RequestMapping("/regist")
public String save(UserPO userPO,RedirectAttributes attributes) {
    // 獲取使用者名稱
    
String name=userPO.getName();
    UserPO user=userService.getName(name);
    if(null!=user){
        attributes.addFlashAttribute("mess","使用者名稱已被註冊");
       return  "redirect:";
   }
    userService.save(userPO);
    return "success";
}

獲取使用者名稱,檢查使用者名稱是否為空(userService.getName(name);

根據userService.getName(name);

@Override
public UserPO getName(String name) {
    return userDao.getName(name);
}

由此到UserPO getName(String name);

重點來了  我標紅一下

以往UserPO findByName(String name);可直接查詢

mybatis則

 

<select id="getName" parameterType="java.lang.String" resultType="UserPO">
    select name,password,sex,age  from USER where name=#{name};
</select>

Sql直接寫在mapper.xml中

 

新增和根據使用者名稱和密碼查詢也都類似。

上面所說是mybatis是xml配置方法。

 

下面說下註解的方法

UserPO getName(String name);

@Select("select * from user where name = #{name}")  

UserPo getName(String name);  

 

void insert(UserPO userPO);

@Insert("insert into user(name,password,age,sex) values(#{name},#{password},#{age},#{sex}))

void insert(UserPO userPO);


UserPO getUser(@Param(value = "name") String name, @Param(value = "password") String password);

 

@Select("SELECT name,password,sex,age  FROM USER WHERE NAME=#{name} AND PASSWORD=#{password})

UserPO getUser(@Param(value = "name") String name, @Param(value = "password") String password);

 

5、聯表查詢

比如檢視文章詳情時候,想查詢釋出人資訊,文章表通過user_iduser表相關聯

實體類裡面加上作者資訊

並相應個getset方法。

Dao裡面直接根據文章id去查詢,

對應的NewsMapper.xml則如下


最後查詢到的結果渲染則:

 
下載地址 https://download.csdn.net/download/u010674395/10512244