遇到的问题:
1.原因是在启动类上只有一个@MapperScan注解。需要配置这个注解的basePackages。
@MapperScan(basePackages = {"com.chenxin.springboot_0702"})
之后删除掉@EnableAutoConfiguration和@ComponentScan(basePackages = {"com.chenxin.springboot_0702"})也照样运行成功。
第二个问题:测试类 里面用@Autowired注解自动装配UserMapper时会有提示报错,但是执行起来没有问题,可以通过。
改成@Resource注解则没有红线了。
首先配置pom.xml文件。springboot的项目都是jar包,不是war包。还有就是依赖模块spring-boot-starter-jdbc不需要,因为在mybatis-spring-boot-starter中已经包含了。
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"> 4.0.0 com.chenxin springboot_0702 0.0.1-SNAPSHOT jar springboot_0702 Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.9.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-test test
mysql mysql-connector-java com.alibaba druid 1.0.5 org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.0 com.github.pagehelper pagehelper-spring-boot-starter 1.1.1 org.springframework.boot spring-boot-maven-plugin
application.properties
#配置mysql的连接配置
spring.datasource.url=jdbc:mysql://localhost:3306/testx
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
接着是model类。
| users | CREATE TABLE `users` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(45) NOT NULL,
`password` varchar(145) NOT NULL,
`name` varchar(45) DEFAULT NULL,
`courseId` int(11) DEFAULT NULL,
`image` varchar(150) DEFAULT NULL,
`email` varchar(45) DEFAULT NULL,
`address` varchar(45) DEFAULT NULL,
`phone` varchar(45) DEFAULT NULL,
`createdAt` bigint(20) DEFAULT NULL,
`updatedAt` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`) USING HASH
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8 |
import java.io.Serializable;
public class User implements Serializable { private long id; private String userName; private String password; private String name; private int courseId; private String image; private String email; private String address; private String phone; private long createdAt; protected long updatedAt; public User() { } //其他getter和setter
//toString方法 }
然后是mapper类。
import com.chenxin.springboot_0702.model.User; import org.apache.ibatis.annotations.*; import org.apache.ibatis.mapping.StatementType; import java.util.List; import java.util.Map; @Mapper public interface UserMapper { @Select("select * from users") public List
接着说springboot启动类。最开始提过了在这里只用了springboot整合mybatis和mysql。所以@ComponentScan和@EnableAutoConfiguration。
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication //开启通用注解扫描 //@ComponentScan(basePackages = {"com.chenxin.springboot_0702"}) @MapperScan(basePackages = {"com.chenxin.springboot_0702"}) //@EnableAutoConfiguration public class Run { public static void main(String[] args) { SpringApplication.run(Run.class, args); } }
测试类
import com.chenxin.springboot_0702.dao.UserMapper; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; @RunWith(SpringRunner.class) @SpringBootTest(classes = Run.class) @EnableAutoConfiguration public class RunTests { @Resource private UserMapper userMapper; @Test public void contextLoads() { } @Test public void G() { System.out.println(userMapper.findAll()); } }
接下来是其他的增删改查。其中更新使用的@Update注解,貌似可以自动判断是否为空,只要为空就不更新了。
import com.chenxin.springboot_0702.model.User; import org.apache.ibatis.annotations.*; import org.apache.ibatis.mapping.StatementType; import java.util.List; import java.util.Map; @Mapper public interface UserMapper { //查询 @Select("select * from users where id = #{id}") public User findById(@Param("id") long id) throws Exception; @Select("select * from users") public ListfindAll() throws Exception; @Select("select * from users where phone=#{phone}") public List findByPhone(@Param("phone") String phone) throws Exception; //新增 @Insert("INSERT INTO users(id,username,password,name,courseId,image,email,address,phone,createdAt,updatedAt) VALUES (#{id},#{username},#{password},#{name},#{courseId},#{image},#{email},#{address},#{phone},#{createdAt},#{updatedAt})") @Options(useGeneratedKeys = true, keyProperty = "id") public int add(User user) throws Exception; //删除 @Delete("delete from users where id = #{id}") public boolean delete(@Param("id") long id) throws Exception; //更新 @Update("update users set username=#{username}, password=#{password}, name=#{name}, courseId=#{courseId}, image=#{image}, email=#{email}, address=#{address}, phone=#{phone}, updatedAt=#{updatedAt} where id=#{id}") public boolean update(User user) throws Exception; }
测试单元
import com.chenxin.springboot_0702.dao.UserMapper; import com.chenxin.springboot_0702.model.User; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest(classes = Run.class) @EnableAutoConfiguration public class RunTests { @Resource private UserMapper userMapper; @Test public void G() throws Exception { System.out.println(userMapper.findAll()); } @Test public void g1() throws Exception { System.out.println(userMapper.findById(12L)); } @Test public void g3() throws Exception { User user = new User(); user.setName("无极"); user.setUsername("六六qijiu"); user.setPassword("12443345"); userMapper.add(user); System.out.println(user.getId()); } @Test //删除 public void g4() throws Exception{ long id = 14; System.out.println(userMapper.delete(id)); } @Test //更新 public void g5() throws Exception{ User user = userMapper.findById(16); user.setUsername("七七七"); System.out.println(userMapper.update(user)); } @Test public void g6() throws Exception{ Listusers = userMapper.findByPhone("13101436674"); for (User user : users) System.out.println(user.getId() + "***" + user.getPhone()); } }
原文地址:https://www.cnblogs.com/JasonChen92/p/9259629.html