项目结构
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<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>
<version>5.1.22</version>
</dependency>
User.java
package com.fengqing.aajspringbootmybatis.bean;
public class User {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserMapper.java
package com.fengqing.aajspringbootmybatis.dao;
import com.fengqing.aajspringbootmybatis.bean.User;
import org.apache.ibatis.annotations.Param;
public interface UserMapper {
public User get(@Param("id") Integer id);
}
UserService.java
package com.fengqing.aajspringbootmybatis.service;
import com.fengqing.aajspringbootmybatis.bean.User;
public interface UserService {
public User getUser(Integer id);
}
UserServiceImpl.java
package com.fengqing.aajspringbootmybatis.service.impl;
import com.fengqing.aajspringbootmybatis.bean.User;
import com.fengqing.aajspringbootmybatis.dao.UserMapper;
import com.fengqing.aajspringbootmybatis.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public User getUser(Integer id) {
return this.userMapper.get(id);
}
}
UserController.java
package com.fengqing.aajspringbootmybatis.controller;
import com.fengqing.aajspringbootmybatis.bean.User;
import com.fengqing.aajspringbootmybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/get")
public String get(Integer id) {
User user = this.userService.getUser(id);
return user.getUsername() + "--" + user.getPassword();
}
}
启动类AajspringbootMybatisApplication.java
package com.fengqing.aajspringbootmybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.fengqing.aajspringbootmybatis.dao")
public class AajspringbootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(AajspringbootMybatisApplication.class, args);
}
}
UserMapper.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.fengqing.aajspringbootmybatis.dao.UserMapper">
<select id="get" resultType="user" parameterType="int">
select * from users where id = #{id}
</select>
</mapper>
application.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.126.133:3306/mydb
spring.datasource.username=root
spring.datasource.password=123456
##指定mpper文件的位置
mybatis.mapper-locations=classpath:mapper/UserMapper.xml
##为实体类起别名
mybatis.type-aliases-package=com.fengqing.aajspringbootmybatis.bean
数据库mydb有个users表
启动springboot,打开浏览器访问 http://localhost:8080/user/get?id=1