SpringBoot當(dāng)中如何整合mybatis和注入
整合mybatis和注入
馬克-to-win@馬克java社區(qū):
根據(jù)第3部分的helloworld例子,用那個(gè)項(xiàng)目做底子。pom.xml只需要加入mybatis和mysql的部分,另外注意版本要比helloworld的高,比如要1.5.9版本以上。 參見我的項(xiàng)目bootMybatis1。
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號: 73203。
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<build />
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- after change class,automatic restart tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
index
<a href="/helloa.do">test mybatis</a>
</body>
</html>
package com;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.mapper.RegisterMapper;
@Controller
public class HelloWorldController {
@Resource
private RegisterMapper registerMapper;
@RequestMapping("/helloa")
public void helloWorld(HttpServletResponse res) throws IOException {
Register register = registerMapper.selectByPrimaryKey(1);
System.out.println("馬克-to-win @馬克java社區(qū) is "+register.toString());
res.sendRedirect("index.html");
}
}
package com.mapper;
import com.Register;
public interface RegisterMapper {
Register selectByPrimaryKey(Integer id);
}
以下@MapperScan和@ComponentScan會(huì)到相應(yīng)的目錄中實(shí)例化對象。
package com.SpringbootMaven;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@MapperScan(value = "com.mapper")
@ComponentScan({"com"})
public class App {
public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}
}
RegisterMapper.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.mapper.RegisterMapper">
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultType="com.Register">
select Id, Name, Age from register
where Id = #{id,jdbcType=INTEGER}
</select>
</mapper>
最重要的配置文件:
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=123
mybatis.mapper-locations=classpath:/mapper/*Mapper.xml