项目地址:github地址
- 创建项目
- 配置pom.xml
- mybatis配置
- main函数创建 application.yml文件创建
- 单元测试
1.创建一个maven项目
文件目录格式:
创建
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.hiro</groupId>
<artifactId>springbootDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.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.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.41</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>
如果对应依赖在当前工程还是不能正确引用,右击pom.xml-》Maven-》Reimport
mybatis引入
mybatis可通过注解和xml引入,这里主要介绍xml配置。
创建UserMapper.java
package com.hiro.demo.db.dao;
import com.hiro.demo.db.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* Project: springbootDemo
*
* @author : hirolin
* @date : 2019/3/25 22:14
*/
@Mapper
public interface UserMapper {
User getUser(@Param("id") int id);
}
创建User.java
package com.hiro.demo.db.entity;
/**
* Project: springbootDemo
*
* @author : hirolin
* @date : 2019/3/25 22:14
*/
public class User {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
创建xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hiro.demo.db.dao.UserMapper">
<resultMap type="com.hiro.demo.db.entity.User" id="userMap">
<id column="id" property="id" />
<result column="name" property="name" jdbcType="VARCHAR"/>
</resultMap>
<select id="getUser" resultMap="userMap">
select * from user where id = #{id}
</select>
</mapper>
main函数创建 application.yml文件创建
package com.hiro.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* Project: springbootDemo
*
* @author : hirolin
* @date : 2019/3/25 22:13
*/
@SpringBootApplication
@MapperScan("com.hiro.demo.db")
public class DemoApplication {
public static void main(String [] args) {
SpringApplication.run(DemoApplication.class,args);
}
}
application.yml
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/demo
username: root
password: xxxxx #自己的数据库配置
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*xml
单元测试
package com.hiro.demo;
import com.hiro.demo.db.dao.UserMapper;
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.SpringJUnit4ClassRunner;
/**
* Project: springbootDemo
*
* @author : hirolin
* @date : 2019/3/25 22:15
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class DemoApplicationTest {
@Autowired
private UserMapper userMapper;
@Test
public void Test(){
System.out.printf(userMapper.getUser(1).getName());
}
}
完整目录结构:
在这里插入图片描述
网友评论