Springboot web入门搭建 整合mybatis+junit4+swagger+thymeleaf+druid+redis(二)

                              springboot集成mybatis+junit4

1.先在数据库中创建user表,用于测试mybatis是否搭建成功

CREATE TABLE `user` (
	`id`  int NOT NULL AUTO_INCREMENT ,
	`passwd`  varchar(255) NULL ,
	`username`  varchar(255) NULL ,
	PRIMARY KEY (`id`)
);

插入一条数据:

INSERT into user (id,passwd,username) VALUES(1,"admin","liheng")

2.修改上篇文章工程的启动类

引入pom.xml依赖

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
    </parent>
	  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
	<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

创建com.liheng.App.class

@SpringBootApplication
public class App {
    public static void main(String[] args) throws Exception {
    	SpringApplication.run(App.class, args);
    }
}

创建com.liheng.controller.UserController.class

@RestController
public class UserController {

    @RequestMapping("/hello")
    public Object sayHello() {
        return "hello";
    }
}

运行App,在浏览器输入:localhost:8080/hello,发现“hello”说明第一步部署成功。

3.集成mybatis

3.1 pom.xml文件中添加mybatis的依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.2.0</version>
</dependency>

<dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
  </dependency>

3.2 生成mapper文件

3.2.1 在resources目录,新建application.yml文件,内容如下

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    #这里的数据库配置为对应自己的
    url: jdbc:mysql://127.0.0.1:3306/sbdb?serverTimezone=GMT%2B8 
    username: root
    password: root
mybatis:
  mapper-locations: classpath:mapping/*.xml

3.2.2 准备mybatis的生成文件generatorConfig.xml,并在相应目录创建好model,dao,mapping文件

generatorConfig.xml 文件内容

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
    <classPathEntry  location="F:\Maven_Repo\mysql\mysql-connector-java\5.1.47\mysql-connector-java-5.1.47.jar"/>
    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/sbdb" userId="root" password="root">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成模型的包名和位置-->
        <javaModelGenerator targetPackage="com.liheng.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置-->
        <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.liheng.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
        <table tableName="user" domainObjectName="Users" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

右键generatorConfig.xml文件,点击运行,生成结构如下:

如果右键没有run按钮,说明没有安装mybatis plugin 这个插件,可以参考以下博客安装

https://blog.csdn.net/u014365133/article/details/78885189

3.2.3 修改App.class启动类,增加@MapperScan扫描注解

@SpringBootApplication
@MapperScan("com.liheng.dao")
public class App {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(App.class, args);
    }
}

4.添加SpringBoot单元测试

4.1 pom.xml文件中添加依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
</dependency>

4.2 创建UserTest.class测试类

@SpringBootTest(classes={App.class})
@RunWith(SpringRunner.class)
public class UserTest {
	@Resource
	private UsersMapper usersMapper;

	@Test
	public void testFindUser() {
		Users user = usersMapper.selectByPrimaryKey(1);
		System.out.println("userName = " + user.getUsername());
	}

}

打印结果如下,说明mybatis访问数据库成功:

至此,springboot集成mybatis+junit4完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半路笙歌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值