学习springboot整合mybatis并编写测试类

遇到的问题:

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中已经包含了。

复制代码
<?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>
&lt;groupId&gt;com.chenxin&lt;/groupId&gt;
&lt;artifactId&gt;springboot_0702&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;packaging&gt;jar&lt;/packaging&gt;

&lt;name&gt;springboot_0702&lt;/name&gt;
&lt;description&gt;Demo project <span style="color: #0000ff;">for</span> Spring Boot&lt;/description&gt;

&lt;parent&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
    &lt;version&gt;1.5.9.RELEASE&lt;/version&gt;
    &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;

&lt;properties&gt;
    &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
    &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
    &lt;java.version&gt;1.8&lt;/java.version&gt;
&lt;/properties&gt;

&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
        &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;!-- Junit依赖 --&gt;
    <span style="color: #808080; font-style: italic;">&lt;!--junit<span style="color: #808080; font-style: italic; font-family: '宋体';">不需要<span style="color: #808080; font-style: italic;">springboot<span style="color: #808080; font-style: italic; font-family: '宋体';">已经自动加载了。<span style="color: #808080; font-style: italic;">--&gt;<br></span></span></span></span></span></pre>
     <!--spring-boot-starter-jdbc已经包含在了mybatis-spring-boot-starter中了。-->
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.5</version> </dependency> <!--引入mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <!--mybatis分页插件--> <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
复制代码

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;
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> User() {
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">其他getter和setter<br>   //toString方法</span>

}

复制代码

然后是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(</span>"select * from users"<span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span> List&lt;Map&gt;<span style="color: #000000;"> findAll();


}

复制代码

接着说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 {

</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) {
    SpringApplication.run(Run.</span><span style="color: #0000ff;">class</span><span style="color: #000000;">, 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
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> contextLoads() {
}


@Test
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> 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 {

</span><span style="color: #008000;">//</span><span style="color: #008000;">查询</span>
@Select("select * from users where id = #{id}"<span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span> User findById(@Param("id") <span style="color: #0000ff;">long</span> id) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception;

@Select(</span>"select * from users"<span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span> List&lt;User&gt; findAll() <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception;

@Select(</span>"select * from users where phone=#{phone}"<span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span> List&lt;User&gt; findByPhone(@Param("phone") String phone) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception;

</span><span style="color: #008000;">//</span><span style="color: #008000;">新增</span>
@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})"<span style="color: #000000;">)
@Options(useGeneratedKeys </span>= <span style="color: #0000ff;">true</span>, keyProperty = "id"<span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span> add(User user) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception;

</span><span style="color: #008000;">//</span><span style="color: #008000;">删除</span>
@Delete("delete from users where id = #{id}"<span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">boolean</span> delete(@Param("id") <span style="color: #0000ff;">long</span> id) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception;

</span><span style="color: #008000;">//</span><span style="color: #008000;">更新</span>
@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}"<span style="color: #000000;">)
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">boolean</span> update(User user) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> 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
</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> UserMapper userMapper;

@Test
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> G() <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception {
    System.out.println(userMapper.findAll());
}
@Test
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> g1() <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception {
    System.out.println(userMapper.findById(</span>12L<span style="color: #000000;">));
}
@Test
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> g3() <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception {
    User user </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> User();
    user.setName(</span>"无极"<span style="color: #000000;">);
    user.setUsername(</span>"六六qijiu"<span style="color: #000000;">);
    user.setPassword(</span>"12443345"<span style="color: #000000;">);
    userMapper.add(user);
    System.out.println(user.getId());
}
@Test
</span><span style="color: #008000;">//</span><span style="color: #008000;">删除</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> g4() <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception{
    </span><span style="color: #0000ff;">long</span> id = 14<span style="color: #000000;">;
    System.out.println(userMapper.delete(id));
}

@Test
</span><span style="color: #008000;">//</span><span style="color: #008000;">更新</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> g5() <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception{
    User user </span>= userMapper.findById(16<span style="color: #000000;">);
    user.setUsername(</span>"七七七"<span style="color: #000000;">);
    System.out.println(userMapper.update(user));
}
@Test
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> g6() <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception{
    List</span>&lt;User&gt; users = userMapper.findByPhone("13101436674"<span style="color: #000000;">);
    </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (User user : users)
        System.out.println(user.getId() </span>+ "***" +<span style="color: #000000;"> user.getPhone());
}

}

复制代码

原文地址:https://www.cnblogs.com/JasonChen92/p/9259629.html

posted @ 2019-08-19 10:14  星朝  阅读(1148)  评论(0编辑  收藏  举报