SpringBoot当中如何整合mybatis和注入

整合mybatis和注入
马克-to-win@马克java社区: 根据第3部分的helloworld例子,用那个项目做底子。pom.xml只需要加入mybatis和mysql的部分,另外注意版本要比helloworld的高,比如要1.5.9版本以上。 参见我的项目bootMybatis1。
马克- to-win:马克 java社区:防盗版实名手机尾号: 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社区  is "+register.toString());
        res.sendRedirect("index.html");
    }
}





package com.mapper;
import com.Register;
public interface RegisterMapper {
    Register selectByPrimaryKey(Integer id);
}

以下@MapperScan和@ComponentScan会到相应的目录中实例化对象。


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