時隔兩個月的再來寫博客的感受怎麼樣呢,只能用「棒」來形容了。閒話少說,直接入正題,以前的博客中有說過,將spring與mybatis整個後開發會更爽,基於如今springboot已經成爲整個業界開發主流框架的狀況下,今天在這裏就直接將mybatis整合spring boot了。java
先簡單地提一下Spring boot。在Mybatis還沒火起來以前,你們用的是SSH(Struts2+Spring+Hibernate),以後mybatis以其小巧輕便的優勢成爲中小型項目的首選,與此同時基於Spring的自家mvc框架SpringMVC也火起來了,因而開發的框架又成了SSM(SpringMVC+Spring+Mybatis),可是Spring和Spring MVC本是同源,叫起來還得分開叫真是頭疼,而後Spring boot出現了,它是基於Spring4的條件註冊的一套快速開發整合包,同時又整合了Spring MVC了,因此說SpringMVC的那一套註解能夠原封不動地搬來用。同時Spring boot爲了解決Spring框架須要進行大量的配置的問題又引入自動配置的概念,也就是說能用註解我毫不用配置文件。關於Spring boot具體一點的東西我就直接在下面結合代碼裏講了。mysql
首先呢,新建一個maven項目,記得勾選上create a simple project
web
以後的Group id、Artifact Id之類的就能夠隨便填了,如下是我建好的項目結構spring
項目建好後第一件事,添加依賴sql
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
解釋一下幾個關鍵的包數據庫
包導完以後個人習慣是先把整個項目的包結構搭建起來apache
而後在最外層的包裏面寫啓動類,老規矩先貼代碼tomcat
package com.fiberhome; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
這裏只有一個註解@SpringBootApplication,但是做用卻大得驚人,control鍵而後點擊該註解看源碼可知它替代了@@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan這三個註解的功能。接下來解釋這個三個註解的做用,理解了這三個註解,天然理解了@SpringBootApplication。springboot
Springboot能夠
根據你添加的jar包來配置你項目的默認配置,好比當你添加了mvc的jar包,它就會自動配置web項目所需的配置<context:component-scan>,
該註解不填屬性的話就是默認掃描啓動類所在的包,或者啓動類所在包的下一級,因此啓動類要放在最外層緊接着把個人實體類(User.java)代碼貼出來mybatis
package com.fiberhome.pojo; import org.springframework.stereotype.Component; @Component public class User { private Long id; private String username; private int age; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
而後咱們從底層的代碼寫起,先是mapper接口
package com.fiberhome.mapper; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.fiberhome.pojo.User; @Mapper public interface UserMapper { //獲取用戶名單 public List<User> getUser() throws Exception; //根據id刪除用戶 public void deleteUser(int id)throws Exception; //新增用戶 public void addUser(User user)throws Exception; }
而後是對應該mapper接口的user.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://www.mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.fiberhome.mapper.UserMapper"> <select id="getUser" resultType="com.fiberhome.pojo.User"> select * from user </select> <delete id="deleteUser" parameterType="Integer"> delete from user where id =#{id} </delete> <insert id="addUser" parameterType="com.fiberhome.pojo.User"> insert into user(id,username,age)values(#{id},#{username},#{age}) </insert> </mapper>
緊接着是Service層的代碼
UserService.java
package com.fiberhome.service; import java.util.List; import com.fiberhome.pojo.User; public interface UserService { //顯示全部用戶 public List<User>getUser()throws Exception; //根據id刪除用戶 public void deleteUser(int id)throws Exception; //新增用戶 public void addUser(User user)throws Exception; }
UserServiceImpl.java
package com.fiberhome.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.fiberhome.mapper.UserMapper; import com.fiberhome.pojo.User; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public List<User> getUser() throws Exception { return userMapper.getUser(); } //根據id刪除用戶 @Override public void deleteUser(int id) throws Exception { userMapper.deleteUser(id); } //新增用戶 @Override public void addUser(User user) throws Exception { userMapper.addUser(user); } }
最後是Controller代碼
package com.fiberhome.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.fiberhome.pojo.User; import com.fiberhome.service.UserService; @RestController public class UserController { @Autowired private UserService userService; @Autowired private User user; //顯示用戶 @RequestMapping("list") public List<User> index() throws Exception { return userService.getUser(); } //刪除用戶 @RequestMapping("delete/{id}") public String delete(@PathVariable int id) throws Exception { userService.deleteUser(id); return "你已經刪掉了id爲"+id+"的用戶"; } //增長用戶 @RequestMapping("addUser") public String addUser() throws Exception { user.setAge(33); user.setUsername("阿花"); userService.addUser(user); return "增長用戶"; } }
以上代碼已經寫得很明白了,也沒什麼可解釋的。值得注意的是爲了將mapper裝配到spring容器中去,要在mapper接口中加上@Mapper註解,或者在啓動類中加上@MapperScan(「包路徑」)註解。到了這裏基本完工了,還差一個application.properties文件,用於存放數據庫鏈接信息和mapper.xml文件位置
spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=123456 mybatis.mapper-locations: classpath:mapper/*.xml
運行的時候只需運行啓動類的main方法便可,因爲springboot嵌入了tomcat,因此項目跑起來後tomcat會自啓。
到這裏mybatis和springboot就整合完了,有人可能會想,既然省了這麼多xml文件,有沒有方法能夠連user.xml文件一塊兒省了,答案是固然有了,接下來我還會帶來mybatis註解開發,敬請期待。。。