1. 程式人生 > >springboot同mybatis整合

springboot同mybatis整合

role style 實現 bsp return 最大連接數 typeid localhost 做的

springboot和mybatis整合有兩種開發模式,首先要做的是配置好開發環境,

實現步驟:

在maven文件pom中配置:

1)SpringBoot同Mybatis整合的依賴。

<dependency>
<groupId>com.ruijc</groupId>
<artifactId>spring-boot-starter-mybatis</artifactId>
<version>3.2.2</version>
</dependency>

2)SpringBoot操作數據庫需要數據庫的jar包(如mysql的jar包),還有需要連接池的jar包。

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.2</version>
</dependency
>

3)肯定需要做jdbc屬性配置:

包含連接字符串,用戶名,密碼,最大連接數等

server:
address:
192.168.31.212
port: 10090
context-path: /three
spring:
datasource:
driver-class-name:
com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/bookonline
username: root
password: 12345678

第一種:

註解開發:

1)通過註解的方式開發Mapper接口

 1  package com.jinglin.dao.imp;
 2 
 3 
 4 
 5 import com.jinglin.model.ManagerUser;
 6 
 7 import org.apache.ibatis.annotations.Insert;
 8 
 9 import org.apache.ibatis.annotations.Mapper;
10 
11 import org.apache.ibatis.annotations.Param;
12 
13 
14 
15 /**
16 
17  * Created by sony on 2017/8/2.
18 
19  */
20 
21 @Mapper
22 
23 public interface ManagerUserMapper {
24 
25     @Insert("insert into manageruser(username,userpwd,roleid)" +
26 
27             "values(#{managerUser.username},#{managerUser.userpwd},#{managerUser.roleid})")
28 
29     public int additem(@Param("managerUser")ManagerUser managerUser);
30 
31 }

2)開發service層,之前開發的serivice

3)開發controller層,跟之前開發的controller層一樣

第二種:

配置開發模式:

配置mybatis的相關的屬性。在resource文件夾下創建一個新的文件夾,該文件夾用以存放mybatis的mapper映射文件。另外就需要在yml配置mybatis的屬性:

server:
address:
192.168.31.212
port: 10090
context-path: /three
spring:
datasource:
driver-class-name:
com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/bookonline
username: root
password: 12345678
mybatis:
mapper-locations:
classpath*:mybatis/*Mapper.xml

type-aliases-package: com.jinglin.model

其中mapper-locations表示的是mapper映射文件路徑

type-aliases-package:數據庫映射的實體包名

1)開發dao層:

public interface BookTypeDao extends IDaoShop<BookType> {
}

2)配置映射文件:

1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
3         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
4 <mapper namespace="com.jinglin.dao.imp.BookTypeDao">
5     <insert id="additem" parameterType="BookType">
6         insert into booktype(typename)values(#{typename})
7     </insert>
8 </mapper>

3)將所有的dao層的操作接口全部掃描到spring中,掃描的方式是在啟動類中加入註解:

@SpringBootApplication
@MapperScan("com.jinglin.dao.imp")
public class ThreedemoApplication {

public static void main(String[] args) {
SpringApplication.run(ThreedemoApplication.class, args);
}
}

紅色標記的就表示將哪個包掃描到spring中。

4)後面的開發同之前的一樣,開發service層,開發controller層。

5) 如果需要在service層的方法裏加入事務。采取的是註解的事務開發:

[email protected]

 1 @Service
 2 public class BookTypeService {
 3     @Autowired
 4     private BookTypeDao bookTypeDao;
 5     @Transactional
 6     public int additem(BookType bookType){
 7         int i=bookTypeDao.additem(bookType);
 8         BookType booktype2 = new BookType();
 9         booktype2.setTypeid(10);
10         booktype2.setTypename("aaddbddddddweweeeee");
11         i+=bookTypeDao.updateitem(booktype2);
12         return i;
13 
14     }
15 }

總結:springboot開發同ssm基礎是一樣的,也要搭配環境,首先就是類似springboot同mybatis的整合,1、創建依賴 2、導入操作數據庫的jar包 3、做jdbc的連接配置包括連接字符串、用戶名,用戶密碼等。做xml配置文件,需要先創建文件夾存儲。必須在yml中配置mybatis。

不同點:[email protected] 在springboot中可以用RestController來將其代替。

springboot同mybatis整合