先看pom.xml中的配置吧
<?xml version="1.0" encoding="UTF-8"?>
<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.rosam</groupId>
<artifactId>ywt</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis相关 -->
<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>
在看application.properties中配置mysql,和mybatis中的mapper路径,和实体路径(resources文件夹就可)
#数据库设置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ywttest
spring.datasource.username=root
spring.datasource.password=root
#mybatis_config
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.rosam.ywttest.entity
controller层的代码(com.rosam.ywttest.controller)
package com.rosam.ywttest.controller;
import com.rosam.ywttest.entity.User;
import com.rosam.ywttest.service.YwtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.xml.ws.Action;
@Controller
public class YwtController {
@Autowired
private YwtService ywtService;
@RequestMapping("/ywt/{id}")
public void test(@PathVariable int id){
User user=ywtService.find(id);
System.out.println(user.getName());
}
}
service层的代码:(com.rosam.ywttest.service)
package com.rosam.ywttest.service;
import com.rosam.ywttest.dao.YwtDAO;
import com.rosam.ywttest.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class YwtService {
@Autowired
private YwtDAO ywtDAO;
public User find(int id){
User user=ywtDAO.find(id);
return user;
}
}
dao层的代码(com.rosam.ywttest.dao)
package com.rosam.ywttest.dao;
import com.rosam.ywttest.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface YwtDAO {
User find(@Param("id") int id);
}
在resource/mapper下的xml文件
YwtMapper.xml。—–这里需要注意的是id要和dao.java中的方法名一致,namespace和YwtDAO一致。
<?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.rosam.ywttest.dao.YwtDAO">
<select id="find" resultType="User">
select * FROM ywtuser WHERE id=#{id};
</select>
</mapper>
然后是启动类,启动类一定在类上进行注明dao的位置
package com.rosam.ywttest;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.rosam.ywttest.dao")
public class MyywttestApplication {
public static void main(String[] args){
System.out.println("你好");
SpringApplication.run(MyywttestApplication.class,args);
}
}
值得注意的是,MyywtApplication的位置是和controller,service,dao同级的,这样启动MyywtApplication时就可以把同级的和子包中的文件一起加载进来。