欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot 整合mybatis

程序员文章站 2022-05-26 11:30:08
...

springboot 注解的方式整合mybatis

  • 新建一个springboot项目
    springboot 整合mybatis

  • 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);
	}

}

(~ ̄▽ ̄)~ 结束...

相关标签: springboot mybatis