提交 3e3ad701 编写于 作者: 微笑很纯洁's avatar 微笑很纯洁

Spring Boot MyBatis Demo

上级 4f1a3d66
mybatis.type-aliases-package=com.neo.entity
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = root
mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis.type-aliases-package=com.neo.entity
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = root
......@@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
......@@ -24,15 +24,6 @@
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
......@@ -40,47 +31,24 @@
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>bootstrap-test.properties</exclude>
<exclude>bootstrap-dev.properties</exclude>
<exclude>bootstrap-pro.properties</exclude>
<exclude>bootstrap.properties</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>bootstrap-${env}.properties</include>
<include>bootstrap.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
......
......@@ -6,9 +6,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.neo.mapper")
public class Application {
public class MybatisAnnotationApplication {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
SpringApplication.run(MybatisAnnotationApplication.class, args);
}
}
......@@ -2,6 +2,7 @@ package com.neo.mapper;
import java.util.List;
import com.neo.model.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Result;
......@@ -9,7 +10,6 @@ import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.neo.entity.UserEntity;
import com.neo.enums.UserSexEnum;
public interface UserMapper {
......@@ -19,20 +19,20 @@ public interface UserMapper {
@Result(property = "userSex", column = "user_sex", javaType = UserSexEnum.class),
@Result(property = "nickName", column = "nick_name")
})
List<UserEntity> getAll();
List<User> getAll();
@Select("SELECT * FROM users WHERE id = #{id}")
@Results({
@Result(property = "userSex", column = "user_sex", javaType = UserSexEnum.class),
@Result(property = "nickName", column = "nick_name")
})
UserEntity getOne(Long id);
User getOne(Long id);
@Insert("INSERT INTO users(userName,passWord,user_sex) VALUES(#{userName}, #{passWord}, #{userSex})")
void insert(UserEntity user);
void insert(User user);
@Update("UPDATE users SET userName=#{userName},nick_name=#{nickName} WHERE id =#{id}")
void update(UserEntity user);
void update(User user);
@Delete("DELETE FROM users WHERE id =#{id}")
void delete(Long id);
......
package com.neo.entity;
package com.neo.model;
import java.io.Serializable;
import com.neo.enums.UserSexEnum;
public class UserEntity implements Serializable {
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
......@@ -13,11 +13,11 @@ public class UserEntity implements Serializable {
private UserSexEnum userSex;
private String nickName;
public UserEntity() {
public User() {
super();
}
public UserEntity(String userName, String passWord, UserSexEnum userSex) {
public User(String userName, String passWord, UserSexEnum userSex) {
super();
this.passWord = passWord;
this.userName = userName;
......
......@@ -7,7 +7,7 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.neo.entity.UserEntity;
import com.neo.model.User;
import com.neo.mapper.UserMapper;
@RestController
......@@ -17,24 +17,24 @@ public class UserController {
private UserMapper userMapper;
@RequestMapping("/getUsers")
public List<UserEntity> getUsers() {
List<UserEntity> users=userMapper.getAll();
public List<User> getUsers() {
List<User> users=userMapper.getAll();
return users;
}
@RequestMapping("/getUser")
public UserEntity getUser(Long id) {
UserEntity user=userMapper.getOne(id);
public User getUser(Long id) {
User user=userMapper.getOne(id);
return user;
}
@RequestMapping("/add")
public void save(UserEntity user) {
public void save(User user) {
userMapper.insert(user);
}
@RequestMapping(value="update")
public void update(UserEntity user) {
public void update(User user) {
userMapper.update(user);
}
......
mybatis.type-aliases-package=com.neo.model
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
\ No newline at end of file
......@@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
public class MybatisAnnotationApplicationTests {
@Test
public void contextLoads() {
......
......@@ -2,6 +2,7 @@ package com.neo.mapper;
import java.util.List;
import com.neo.model.User;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -9,7 +10,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.neo.entity.UserEntity;
import com.neo.enums.UserSexEnum;
@RunWith(SpringRunner.class)
......@@ -17,31 +17,31 @@ import com.neo.enums.UserSexEnum;
public class UserMapperTest {
@Autowired
private UserMapper UserMapper;
private UserMapper userMapper;
@Test
public void testInsert() throws Exception {
UserMapper.insert(new UserEntity("aa", "a123456", UserSexEnum.MAN));
UserMapper.insert(new UserEntity("bb", "b123456", UserSexEnum.WOMAN));
UserMapper.insert(new UserEntity("cc", "b123456", UserSexEnum.WOMAN));
userMapper.insert(new User("aa1", "a123456", UserSexEnum.MAN));
userMapper.insert(new User("bb1", "b123456", UserSexEnum.WOMAN));
userMapper.insert(new User("cc1", "b123456", UserSexEnum.WOMAN));
Assert.assertEquals(3, UserMapper.getAll().size());
Assert.assertEquals(3, userMapper.getAll().size());
}
@Test
public void testQuery() throws Exception {
List<UserEntity> users = UserMapper.getAll();
List<User> users = userMapper.getAll();
System.out.println(users.toString());
}
@Test
public void testUpdate() throws Exception {
UserEntity user = UserMapper.getOne(3l);
User user = userMapper.getOne(30l);
System.out.println(user.toString());
user.setNickName("neo");
UserMapper.update(user);
Assert.assertTrue(("neo".equals(UserMapper.getOne(3l).getNickName())));
userMapper.update(user);
Assert.assertTrue(("neo".equals(userMapper.getOne(30l).getNickName())));
}
}
\ No newline at end of file
......@@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
......@@ -24,15 +24,6 @@
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
......@@ -40,50 +31,26 @@
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
<version>2.0.0</version>
</dependency>
<dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>bootstrap-test.properties</exclude>
<exclude>bootstrap-dev.properties</exclude>
<exclude>bootstrap-pro.properties</exclude>
<exclude>bootstrap.properties</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>bootstrap-${env}.properties</include>
<include>bootstrap.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
......@@ -6,9 +6,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.neo.mapper")
public class Application {
public class MybatisXmlApplication {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
SpringApplication.run(MybatisXmlApplication.class, args);
}
}
......@@ -2,17 +2,17 @@ package com.neo.mapper;
import java.util.List;
import com.neo.entity.UserEntity;
import com.neo.model.User;
public interface UserMapper {
List<UserEntity> getAll();
List<User> getAll();
UserEntity getOne(Long id);
User getOne(Long id);
void insert(UserEntity user);
void insert(User user);
void update(UserEntity user);
void update(User user);
void delete(Long id);
......
package com.neo.entity;
package com.neo.model;
import java.io.Serializable;
import com.neo.enums.UserSexEnum;
public class UserEntity implements Serializable {
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
......@@ -13,11 +13,11 @@ public class UserEntity implements Serializable {
private UserSexEnum userSex;
private String nickName;
public UserEntity() {
public User() {
super();
}
public UserEntity(String userName, String passWord, UserSexEnum userSex) {
public User(String userName, String passWord, UserSexEnum userSex) {
super();
this.passWord = passWord;
this.userName = userName;
......
......@@ -7,7 +7,7 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.neo.entity.UserEntity;
import com.neo.model.User;
import com.neo.mapper.UserMapper;
@RestController
......@@ -17,24 +17,24 @@ public class UserController {
private UserMapper userMapper;
@RequestMapping("/getUsers")
public List<UserEntity> getUsers() {
List<UserEntity> users=userMapper.getAll();
public List<User> getUsers() {
List<User> users=userMapper.getAll();
return users;
}
@RequestMapping("/getUser")
public UserEntity getUser(Long id) {
UserEntity user=userMapper.getOne(id);
public User getUser(Long id) {
User user=userMapper.getOne(id);
return user;
}
@RequestMapping("/add")
public void save(UserEntity user) {
public void save(User user) {
userMapper.insert(user);
}
@RequestMapping(value="update")
public void update(UserEntity user) {
public void update(User user) {
userMapper.update(user);
}
......
mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis.type-aliases-package=com.neo.model
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
<?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.neo.mapper.UserMapper" >
<resultMap id="BaseResultMap" type="com.neo.entity.UserEntity" >
<resultMap id="BaseResultMap" type="com.neo.model.User" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="userName" property="userName" jdbcType="VARCHAR" />
<result column="passWord" property="passWord" jdbcType="VARCHAR" />
......@@ -26,7 +26,7 @@
WHERE id = #{id}
</select>
<insert id="insert" parameterType="com.neo.entity.UserEntity" >
<insert id="insert" parameterType="com.neo.model.User" >
INSERT INTO
users
(userName,passWord,user_sex)
......@@ -34,7 +34,7 @@
(#{userName}, #{passWord}, #{userSex})
</insert>
<update id="update" parameterType="com.neo.entity.UserEntity" >
<update id="update" parameterType="com.neo.model.User" >
UPDATE
users
SET
......
......@@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
public class MybatisXmlApplicationTests {
@Test
public void contextLoads() {
......
......@@ -9,7 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.neo.entity.UserEntity;
import com.neo.model.User;
import com.neo.enums.UserSexEnum;
@RunWith(SpringRunner.class)
......@@ -17,20 +17,20 @@ import com.neo.enums.UserSexEnum;
public class UserMapperTest {
@Autowired
private UserMapper UserMapper;
private UserMapper userMapper;
@Test
public void testInsert() throws Exception {
UserMapper.insert(new UserEntity("aa", "a123456", UserSexEnum.MAN));
UserMapper.insert(new UserEntity("bb", "b123456", UserSexEnum.WOMAN));
UserMapper.insert(new UserEntity("cc", "b123456", UserSexEnum.WOMAN));
userMapper.insert(new User("aa", "a123456", UserSexEnum.MAN));
userMapper.insert(new User("bb", "b123456", UserSexEnum.WOMAN));
userMapper.insert(new User("cc", "b123456", UserSexEnum.WOMAN));
Assert.assertEquals(3, UserMapper.getAll().size());
Assert.assertEquals(3, userMapper.getAll().size());
}
@Test
public void testQuery() throws Exception {
List<UserEntity> users = UserMapper.getAll();
List<User> users = userMapper.getAll();
if(users==null || users.size()==0){
System.out.println("is null");
}else{
......@@ -41,11 +41,11 @@ public class UserMapperTest {
@Test
public void testUpdate() throws Exception {
UserEntity user = UserMapper.getOne(6l);
User user = userMapper.getOne(6l);
System.out.println(user.toString());
user.setNickName("neo");
UserMapper.update(user);
Assert.assertTrue(("neo".equals(UserMapper.getOne(6l).getNickName())));
userMapper.update(user);
Assert.assertTrue(("neo".equals(userMapper.getOne(6l).getNickName())));
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册