项目目录:
先贴代码: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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.18.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 添加web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 集成mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- 包含有:MapperScan -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件:application.properties
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=admin
实体类:实体类一定要添加注释@Component,否则无法注射该实体
package com.demo.entity;
import org.springframework.stereotype.Component;
@Component
public class User {
//实体类的属性和表的字段名称一一对应
private int id;
private String name;
private String sex;
private int age;
public User(int id, String name, String sex, int age) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
}
public User() {
// id = -1;
name = null;
sex = null;
age = 0;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex +"]";
}
}
mapper.java
Mapper类需要添加注释:@Mapper,否则提示无法找到UserMapper
package com.demo.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.demo.entity.User;
@Mapper
public interface UserMapper {
//使用@Insert注解指明add方法要执行的SQL
@Insert("insert into users(name, age, sex) values(#{name}, #{age}, #{sex})")
public int add(User user)throws Exception;
//使用@Delete注解指明deleteById方法要执行的SQL
@Delete("delete from users where id=#{id}")
public int deleteById(int id)throws Exception;
//使用@Update注解指明update方法要执行的SQL
@Update("update users set name=#{name},age=#{age} where id=#{id}")
public int updateUser(User user)throws Exception;
//使用@Select注解指明getById方法要执行的SQL
@Select("select * from users where id=#{id}")
public User getById(int id)throws Exception;
//使用@Select注解指明getAll方法要执行的SQL
@Select("select * from users")
public List<User> getAll()throws Exception;
}
Service类:
package com.demo.service;
import java.util.List;
import com.demo.entity.User;
public interface UserService {
//显示所有用户
public List<User>getAll()throws Exception;
//根据id查询用户
public User getById(int id)throws Exception;
//根据id删除用户
public void deleteById(int id)throws Exception;
//新增用户
public void add(User user)throws Exception;
}
Service实现类:
该类需要添加注释:@Service
package com.demo.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo.entity.User;
import com.demo.mapper.UserMapper;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Autowired
private User user;
@Override
public List<User> getAll() throws Exception {
return userMapper.getAll();
}
//根据id查询用户
@Override
public User getById(int id) throws Exception {
return userMapper.getById(id);
}
//根据id删除用户
@Override
public void deleteById(int id) throws Exception {
userMapper.deleteById(id);
}
//新增用户
@Override
public void add(User user) throws Exception {
userMapper.add(user);
}
}
最后,启动类:需要添加注释:@MapperScan,否则无法加载Mapper
package com.demo;
/**
* spring boot启动DemoAplication是需要扫描它下面的Controller等类的
*/
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.mybatis.spring.annotation.MapperScan;
@MapperScan("com.demo.mapper")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
最后一步,@MapperScan注释在“org.mybatis.spring.annotation”包中,刚开始始终无法添加这个注释,后来发现缺少这个包,需要在pom.xml文件中添加载jar包,最终实现SpringBoot+Mybatis的整合。