springboot 整合mybatis
程序员文章站
2022-05-26 11:30:08
...
springboot 注解的方式整合mybatis
-
新建一个springboot项目
-
application.yml配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydata
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
User.java
package com.hll.model;
public class User {
private String id;
private String username;
private String createtime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", createtime=" + createtime + "]";
}
}
Mapper.java
package com.hll.mapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import com.hll.model.User;
@Mapper
public interface UserMapper {
@Select("SELECT * FROM USER WHERE USERNAME = #{username}")
User findUserByUsername(@Param("username") String username);
@Insert("INSERT INTO USER(ID, USERNAME, CREATETIME) VALUES (#{id}, #{username}, #{createtime})")
int insert(@Param("id") String id, @Param("username") String username, @Param("createtime") String createtime);
}
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>
<groupId>com.hll</groupId>
<artifactId>springboot-mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-mybatis</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.14.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
-
SpringbootMybatisApplicationTests
测试
package com.hll;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import com.hll.mapper.UserMapper;
import com.hll.model.User;
import junit.framework.Assert;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMybatisApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
// @Transactional
public void contextLoads() {
User user = userMapper.findUserByUsername("hll");
System.out.println(user.toString());
Assert.assertEquals("hll", user.getUsername());
// int i = userMapper.insert("2", "11111", "2018-07-21");
// int a = 1/0;
// int j = userMapper.insert("3", "22222", "2018-07-21");
//
// System.out.println(i);
}
}
(~ ̄▽ ̄)~ 结束...
推荐阅读
-
mybatis的批量修改 之 ——」执行多条sql报错
-
前后端分离个人博客(Vue+SpringBoot+element ui)
-
SpringBoot整合MyBatis获得插入数据后获取主键,返回值总是1
-
SpringBoot集成JPA
-
MyBatis Generator Example.Criteria 查询条件复制
-
Mybatis笔记06---动态SQL
-
SpringBoot | 第二十七章:监控管理之Actuator使用
-
Spring整合Quartz实现一个简单的定时任务
-
nginx反向代理笔记(1)整合tomcat
-
SpringBoot | 第二十七章:SpringBoot属性配置文件详解