Springboot整合Mysql+Mybatis
沒有廢話,直接開搞!
pom.xml依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 數據庫連接 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
sql
啓動前需要執行sql
CREATE TABLE `jin_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(64) DEFAULT NULL,
`password` varchar(64) DEFAULT NULL,
`name` varchar(64) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `jin_user` (`id`,`user_name`,`password`,`name`) VALUES (1,'lamian','123456','拉麪');
project structure(項目結構)
application.properties
配置SpringBoot啓動配置信息
#項目啓動端口
server.port=1215
#最基礎的數據庫配置
spring.datasource.url=jdbc:mysql://localhost:3306/jin?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#指定mybatis xml文件地址
mybatis.mapperLocations=classpath:com.jin.mapper/*.xml
Application.class
每個springboot都需要一個啓動類.只需要在類上加上註解@SpringBootApplication
(該註解具體介紹http://blog.csdn.net/u013473691/article/details/52353923)
@MapperScan("com.jin.mapper")註解指定mapper接口掃描地址.目的是讓容器能夠從"com.jin.mapper"包下找到mapper接口.
/**
* Created by lamian on 17/6/23.
*/
@SpringBootApplication
@MapperScan("com.jin.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
TestController.class(控制層)
這裏簡單的做了一個登陸的接口,用以調試springboot和mysql&mybatis的整合情況.
此外還有一個啓動測試接口(測試web項目是否運行正常).
/**
* Created by lamian on 17/6/23.
*/
@Controller
@RequestMapping("test")
public class TestController {
@Autowired
private TestService testService;
/**
* 啓動測試接口
* @return
*/
@RequestMapping("/start")
@ResponseBody
public String startTest(){
return "hello, world";
}
/**
* mysql&mybatis整合測試接口
* @return
*/
@RequestMapping("/datasource")
@ResponseBody
public String datasourceTest(@RequestParam(value = "user_name") String userName,
@RequestParam(value = "password") String password){
Map<String, String> mapUser = new HashMap<String, String>();
mapUser.put("userName", userName);
mapUser.put("password", password);
User testUser;
try{
testUser = testService.findUser(mapUser);
}catch (Exception e){
e.printStackTrace();
return "系統錯誤";
}
if(null != testUser){
return "welcome " + testUser.getName();
}
return "賬號密碼錯誤";
}
}
TestService.class(服務層)
一個簡單的service層,直接調mapper接口
/**
* Created by lamian on 17/6/26.
*/
@Service
public class TestService {
@Autowired
private TestMapper testMapper;
public User findUser(Map<String, String> userMap){
if(null == userMap){
return null;
}
User testUser = testMapper.findUser(userMap).get(0);
return testUser;
}
}
TestMapper.interface(mapper接口)
簡單的mapper接口
方法名要跟xml中被調用的sql,id相同.如select標籤的id屬性findUser.
/**
* Created by lamian on 17/6/26.
*/
public interface TestMapper {
List<User> findUser(Map<String, String> userMap);
}
TestMapper.xml(真正需要寫sql的地方)
重要的事情說三遍,仔細仔細一定要仔細。在編寫xml的時候,很容易因爲一些小錯誤導致編譯一直都通不過。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.jin.mapper.TestMapper">
<resultMap id="BaseResult" type="com.jin.entity.User">
<id column="id" property="id"/>
<id column="user_name" property="userName"/>
<id column="password" property="password"/>
<id column="name" property="name"/>
</resultMap>
<sql id="Base_Column_List">
id, user_name, password, name
</sql>
<select id="findUser" resultMap="BaseResult" parameterType="java.util.Map">
SELECT
<include refid="Base_Column_List"/>
from jin_user
<where>
<if test="userName != null">
AND user_name = #{userName}
</if>
<if test="password != null">
AND password = #{password}
</if>
</where>
</select>
</mapper>
打工告成!啓動測試啦
啓動完成Application後,在瀏覽器中輸入
http://localhost:1215/test/datasource?user_name=lamian&password=123456
啪啪啪!當你看到下面的響應時,證明你整合mysql、mybatis成功!
錯誤及解決方案
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException
這是最常見的xml編寫錯誤提示,看到這個一定是你xml文件出錯了(大部分是拼寫錯誤!)
· resultMap標籤下column參數和property參數都對了嗎
· parameterType參數和parameterMap參數使用正確了嗎(不知道就換一下試試=-=)
· mapper標籤namespace屬性是否是相對應的mapper接口
· 還不行?敲個空格在啓一遍試試吧
其它錯誤:
· Application啓動類上面是否添加@MapperScan("你放mapper接口的包名(例如com.jin.mapper)")註解(用於掃描mapper所在接口)
· 配置文件mybatis.mapperLocations=xml文件所在包名(例如classpath:com.jin.mapper/*.xml)
百度谷歌是解決錯誤的最佳選擇.
大多數錯誤都是因爲粗心所引起的.仔細檢查終會發現錯誤.