mybatis--快速入门
This commit is contained in:
33
springboot-mybatis-quickstart/.gitignore
vendored
Normal file
33
springboot-mybatis-quickstart/.gitignore
vendored
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
HELP.md
|
||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### STS ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
88
springboot-mybatis-quickstart/pom.xml
Normal file
88
springboot-mybatis-quickstart/pom.xml
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.inmind</groupId>
|
||||||
|
<artifactId>springboot-mybatis-quickstart</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>springboot-mybatis-quickstart</name>
|
||||||
|
<description>springboot-mybatis-quickstart</description>
|
||||||
|
<properties>
|
||||||
|
<java.version>11</java.version>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
|
<spring-boot.version>2.7.6</spring-boot.version>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<!--mybatis框架起步依赖-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis.spring.boot</groupId>
|
||||||
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
|
<version>2.3.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!--mysql驱动包,操作哪一个数据库,配置对应数据库的驱动-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
<!--之前版本的驱动包,部分企业还在使用-->
|
||||||
|
<!--<dependency>
|
||||||
|
<groupId>com.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>
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-dependencies</artifactId>
|
||||||
|
<version>${spring-boot.version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.8.1</version>
|
||||||
|
<configuration>
|
||||||
|
<source>11</source>
|
||||||
|
<target>11</target>
|
||||||
|
<encoding>UTF-8</encoding>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<version>${spring-boot.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<mainClass>com.inmind.SpringbootMybatisQuickstartApplication</mainClass>
|
||||||
|
<skip>true</skip>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>repackage</id>
|
||||||
|
<goals>
|
||||||
|
<goal>repackage</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.inmind;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class SpringbootMybatisQuickstartApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SpringbootMybatisQuickstartApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.inmind.mapper;
|
||||||
|
|
||||||
|
import com.inmind.pojo.User;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Select;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/*
|
||||||
|
专门操作用户表的Mapper接口
|
||||||
|
*/
|
||||||
|
@Mapper//mybatis在运行时,会自动生成该接口的实现类(代理对象),并且将该对象交给Spring容器管理(Bean对象)
|
||||||
|
public interface UserMapper {
|
||||||
|
@Select("select * from user")
|
||||||
|
public List<User> list();
|
||||||
|
}
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
package com.inmind.pojo;
|
||||||
|
//对应User表的实体类
|
||||||
|
public class User {
|
||||||
|
private Integer id;
|
||||||
|
private String name;
|
||||||
|
private Short age;
|
||||||
|
private Short gender;
|
||||||
|
private String phone;
|
||||||
|
|
||||||
|
public User() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public User(Integer id, String name, Short age, Short gender, String phone) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
this.gender = gender;
|
||||||
|
this.phone = phone;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Integer id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Short getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(Short age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Short getGender() {
|
||||||
|
return gender;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGender(Short gender) {
|
||||||
|
this.gender = gender;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPhone() {
|
||||||
|
return phone;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPhone(String phone) {
|
||||||
|
this.phone = phone;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "User{" +
|
||||||
|
"id=" + id +
|
||||||
|
", name='" + name + '\'' +
|
||||||
|
", age=" + age +
|
||||||
|
", gender=" + gender +
|
||||||
|
", phone='" + phone + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
#下面这些内容是为了让MyBatis映射
|
||||||
|
#指定Mybatis的Mapper文件
|
||||||
|
mybatis.mapper-locations=classpath:mappers/*xml
|
||||||
|
#指定Mybatis的实体目录
|
||||||
|
mybatis.type-aliases-package=com.inmind.mybatis.entity
|
||||||
|
|
||||||
|
#驱动类名称
|
||||||
|
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||||
|
#数据库连接的url
|
||||||
|
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis2
|
||||||
|
#连接数据库的用户名
|
||||||
|
spring.datasource.username=root
|
||||||
|
#连接数据库的密码
|
||||||
|
spring.datasource.password=1234
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package com.inmind;
|
||||||
|
|
||||||
|
import com.inmind.mapper.UserMapper;
|
||||||
|
import com.inmind.pojo.User;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@SpringBootTest//在进行单元测试时,启动spring程序,启动IOC,将程序中Bean对象加载到容器
|
||||||
|
class SpringbootMybatisQuickstartApplicationTests {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserMapper userMapper;//注入的是mybatis自动生成的UserMapper接口类型的实现类对象
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testList(){
|
||||||
|
List<User> userList = userMapper.list();
|
||||||
|
//通过集合获取Stream流,进行遍历消费,user就是集合中的每个元素,拿到每个元素进行打印消费
|
||||||
|
// userList.stream().forEach(user -> System.out.println(user));
|
||||||
|
for (User user : userList) {
|
||||||
|
System.out.println(user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package com.inmind;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class SpringbootWebReqRespApplication {
|
public class SpringbootWebReqRespApplication {
|
||||||
|
|||||||
@@ -6,16 +6,22 @@ import com.inmind.service.EmpService;
|
|||||||
import com.inmind.service.impl.EmpServiceImplA;
|
import com.inmind.service.impl.EmpServiceImplA;
|
||||||
import com.inmind.utils.XmlParserUtils;
|
import com.inmind.utils.XmlParserUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class EmpController {
|
public class EmpController {
|
||||||
|
|
||||||
@Autowired //运行时,IOC容器会提供该类型的bean对象,并赋值给该变量---依赖注入
|
/*@Autowired //运行时,IOC容器会提供该类型的bean对象,并赋值给该变量---依赖注入
|
||||||
|
@Qualifier("empServiceImplB")//当依赖注入EmpService类型的bean对象时,优先使用@Qualifier书写的bean对象名称对应的对象
|
||||||
|
private EmpService empService;*/
|
||||||
|
|
||||||
|
@Resource(name = "empServiceImplA")
|
||||||
private EmpService empService;
|
private EmpService empService;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package com.inmind.dao.impl;
|
|||||||
import com.inmind.dao.EmpDao;
|
import com.inmind.dao.EmpDao;
|
||||||
import com.inmind.pojo.Emp;
|
import com.inmind.pojo.Emp;
|
||||||
import com.inmind.utils.XmlParserUtils;
|
import com.inmind.utils.XmlParserUtils;
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
package com.inmind.service.impl;
|
package com.inmind.service.impl;
|
||||||
|
|
||||||
import com.inmind.dao.EmpDao;
|
import com.inmind.dao.EmpDao;
|
||||||
import com.inmind.dao.impl.EmpDaoImplA;
|
|
||||||
import com.inmind.pojo.Emp;
|
import com.inmind.pojo.Emp;
|
||||||
import com.inmind.service.EmpService;
|
import com.inmind.service.EmpService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package com.inmind.service.impl;
|
||||||
|
|
||||||
|
import com.inmind.dao.EmpDao;
|
||||||
|
import com.inmind.pojo.Emp;
|
||||||
|
import com.inmind.service.EmpService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
//@Component //将当前类交给IOC容器管理,成为IOC容器的bean--控制反转
|
||||||
|
@Service
|
||||||
|
//@Primary//当依赖注入EmpService类型的bean对象时,优先使用EmpServiceImplB
|
||||||
|
public class EmpServiceImplB implements EmpService {
|
||||||
|
@Autowired
|
||||||
|
private EmpDao empDao;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Emp> listEmp() {
|
||||||
|
//业务层从dao层获取数据
|
||||||
|
List<Emp> emps = empDao.listEmp();
|
||||||
|
|
||||||
|
//2.对数据进行转换处理
|
||||||
|
emps.stream().forEach(emp->{
|
||||||
|
//处理性别
|
||||||
|
if (emp.getGender().equals("1")) {
|
||||||
|
emp.setGender("男生");
|
||||||
|
}else{
|
||||||
|
emp.setGender("女生");
|
||||||
|
}
|
||||||
|
//处理职务
|
||||||
|
if (emp.getJob().equals("1")) {
|
||||||
|
emp.setJob("班主任");
|
||||||
|
} else if ("2".equals(emp.getJob())) {
|
||||||
|
emp.setJob("讲师");
|
||||||
|
} else {
|
||||||
|
emp.setJob("辅导员");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return emps;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user