Compare commits
17 Commits
cb31c17ce4
...
main
Author | SHA1 | Date | |
---|---|---|---|
723b51ca3e | |||
1acde5501e | |||
9cb8722bd4 | |||
0ec88d4aa3 | |||
f6d39acad2 | |||
be745c3d1a | |||
ff1aa03490 | |||
784f2e5603 | |||
4040b06ab8 | |||
c49a7571f6 | |||
7228061094 | |||
000eba6f28 | |||
f36634afe2 | |||
ac94e7716a | |||
d36c15438f | |||
da04f6b766 | |||
1935eb352e |
33
springboot-mybatis-crud/.gitignore
vendored
Normal file
33
springboot-mybatis-crud/.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-crud/pom.xml
Normal file
88
springboot-mybatis-crud/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-crud</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>springboot-mybatis-crud</name>
|
||||
<description>springboot-mybatis-crud</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>
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</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>
|
||||
<!--作用:在maven编译时,保留源文件.java中的方法参数名-->
|
||||
<compilerArgs>
|
||||
<arg>-parameters</arg>
|
||||
</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<configuration>
|
||||
<mainClass>com.inmind.SpringbootMybatisCrudApplication</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 SpringbootMybatisCrudApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringbootMybatisCrudApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,68 @@
|
||||
package com.inmind.mapper;
|
||||
|
||||
import com.inmind.pojo.Emp;
|
||||
import org.apache.ibatis.annotations.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface EmpMapper {
|
||||
|
||||
/*
|
||||
员工删除的接口方法
|
||||
delete from emp where id = 17;
|
||||
#{id}:直接将接口方法中传入的参数,使用预编译的方式,动态拼接到sql语句中,实现功能的动态化
|
||||
${id}:直接将参数拼接到sql中
|
||||
*/
|
||||
// @Delete("delete from emp where id = ${id};")
|
||||
// public void delete(Integer id);
|
||||
@Delete("delete from emp where id = #{id};")
|
||||
public int delete(Integer id);
|
||||
|
||||
|
||||
//添加员工
|
||||
@Options(useGeneratedKeys = true,keyProperty = "id")//获取自增长的主键值,赋值给参数Emp对象的id属性
|
||||
@Insert("insert into emp (username, name, gender, image, job, entrydate, dept_id, create_time, update_time)" +
|
||||
"values (#{username},#{name},#{gender},#{image},#{job},#{entrydate},#{deptId},#{createTime},#{updateTime})")
|
||||
public void insert(Emp emp);
|
||||
|
||||
//更新员工
|
||||
/*@Update("update emp set username = #{username}, name = #{name}, gender = #{gender}, image = #{image}, job = #{job}" +
|
||||
", entrydate = #{entrydate}, dept_id = #{deptId},update_time = #{updateTime} where id = #{id}")*/
|
||||
public void update(Emp emp);
|
||||
|
||||
|
||||
//根据id查询员工
|
||||
//属性名与数据表字段名不一致,处理方案一:起别名
|
||||
/*@Select("select id, username, password, name, gender, image, job, entrydate" +
|
||||
", dept_id deptId, create_time createTime, update_time updateTime from emp where id = #{id};")
|
||||
public Emp selectById(Integer id);*/
|
||||
|
||||
|
||||
//属性名与数据表字段名不一致,处理方案二:使用mybatis的2个注解Results和Result,直接告知mybatis哪个字段的值,赋值给哪个属性
|
||||
/*@Results({
|
||||
@Result(column = "dept_id",property = "deptId"),
|
||||
@Result(column = "create_time",property = "createTime"),
|
||||
@Result(column = "update_time",property = "updateTime")
|
||||
})
|
||||
@Select("select id, username, password, name, gender, image, job, entrydate" +
|
||||
", dept_id , create_time, update_time from emp where id = #{id};")
|
||||
public Emp selectById(Integer id);*/
|
||||
|
||||
@Select("select * from emp where id = #{id};")
|
||||
public Emp selectById(Integer id);
|
||||
|
||||
//条件查询员工列表
|
||||
//@Select("select * from emp where name like '%${name}%' and gender = #{gender} and entrydate between #{begin} and #{end} order by update_time desc ;")
|
||||
//注意:#{}替换为?,?不能写到''里面,会报错,可以采用${}解决,也可以使用sql函数concat来解决
|
||||
// public List<Emp> list(@Param("name") String name, @Param("gender") Short gender, @Param("begin") LocalDate begin,@Param("end") LocalDate end);
|
||||
|
||||
/*@Select("select * from emp where name like concat('%',#{name},'%') and gender = #{gender}" +
|
||||
" and entrydate between #{begin} and #{end} order by update_time desc ;")*/
|
||||
public List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);
|
||||
|
||||
|
||||
//批量删除
|
||||
public void deleteByIds(List<Integer> ids);
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
package com.inmind.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Emp {
|
||||
private Integer id;//ID
|
||||
private String username;//用户名
|
||||
private String password;//密码
|
||||
private String name;//姓名
|
||||
private Short gender;//性别 1:男 2:女
|
||||
private String image; //头像URL
|
||||
private Short job; // 职位 1:班主任 2:讲师 3:学工主管 4:教研主管 5:咨询师
|
||||
private LocalDate entrydate; // 入职日期
|
||||
private Integer deptId; // 部门ID
|
||||
private LocalDateTime createTime;
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
#下面这些内容是为了让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/mybatis
|
||||
#连接数据库的用户名
|
||||
spring.datasource.username=root
|
||||
#连接数据库的密码
|
||||
spring.datasource.password=1234
|
||||
|
||||
#输出mybatis的日志
|
||||
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
|
||||
#开启mybatis的驼峰命名自动映射开关,将a_time ---->aTime
|
||||
mybatis.configuration.map-underscore-to-camel-case=true
|
@@ -0,0 +1,77 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.inmind.mapper.EmpMapper">
|
||||
<!--抽取Sql片段:给需要使用Emp查询的sql进行复用(include-refid)-->
|
||||
<sql id="commonEmpSelect">
|
||||
select id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time from emp
|
||||
</sql>
|
||||
|
||||
<!--条件查询操作-->
|
||||
<select id="list" resultType="com.inmind.pojo.Emp">
|
||||
<include refid="commonEmpSelect"></include>
|
||||
<where>
|
||||
<if test="name != null">
|
||||
name like concat('%',#{name},'%')
|
||||
</if>
|
||||
<if test="gender != null">
|
||||
and gender = #{gender}
|
||||
</if>
|
||||
<if test="begin != null and end != null">
|
||||
and entrydate between #{begin} and #{end}
|
||||
</if>
|
||||
</where>
|
||||
order by update_time desc
|
||||
</select>
|
||||
|
||||
<!--动态更新员工信息-->
|
||||
<update id="update">
|
||||
update emp
|
||||
<set>
|
||||
<if test=" username != null">
|
||||
username = #{username},
|
||||
</if>
|
||||
<if test="name != null">
|
||||
name = #{name},
|
||||
</if>
|
||||
<if test="gender != null">
|
||||
gender = #{gender},
|
||||
</if>
|
||||
<if test="image != null">
|
||||
image = #{image},
|
||||
</if>
|
||||
<if test="job != null">
|
||||
job = #{job},
|
||||
</if>
|
||||
<if test="entrydate != null">
|
||||
entrydate = #{entrydate},
|
||||
</if>
|
||||
<if test="deptId != null">
|
||||
dept_id = #{deptId},
|
||||
</if>
|
||||
<if test="updateTime != null">
|
||||
update_time = #{updateTime}
|
||||
</if>
|
||||
</set>
|
||||
|
||||
where id = #{id}
|
||||
</update>
|
||||
|
||||
|
||||
<!--
|
||||
批量删除
|
||||
collection:遍历的集合(接口方法中传入的参数名)
|
||||
item:集合遍历出来的元素
|
||||
separator:分隔符,每个元素之间用什么符号相连
|
||||
open:遍历开始之前拼接的sql片段
|
||||
close:遍历结束之前拼接的sql片段
|
||||
-->
|
||||
<delete id="deleteByIds">
|
||||
<!-- delete from emp where id in (17,18);-->
|
||||
delete from emp where id in
|
||||
<foreach collection="ids" item="id" separator="," open="(" close=")">
|
||||
#{id}
|
||||
</foreach>
|
||||
</delete>
|
||||
</mapper>
|
@@ -0,0 +1,99 @@
|
||||
package com.inmind;
|
||||
|
||||
import com.inmind.mapper.EmpMapper;
|
||||
import com.inmind.pojo.Emp;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
class SpringbootMybatisCrudApplicationTests {
|
||||
@Autowired
|
||||
private EmpMapper empMapper;
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDelete(){
|
||||
int count = empMapper.delete(15);
|
||||
System.out.println(count);
|
||||
};
|
||||
|
||||
@Test
|
||||
void testInsert(){
|
||||
Emp emp = new Emp();
|
||||
emp.setUsername("ls2");
|
||||
emp.setName("李四2");
|
||||
emp.setGender((short) 1);
|
||||
emp.setImage("1.jpg");
|
||||
emp.setJob((short) 1);
|
||||
emp.setEntrydate(LocalDate.of(2005,1,1));
|
||||
emp.setDeptId(1);
|
||||
emp.setCreateTime(LocalDateTime.now());
|
||||
emp.setUpdateTime(LocalDateTime.now());
|
||||
empMapper.insert(emp);
|
||||
System.out.println(emp);
|
||||
System.out.println("主键id:"+emp.getId());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testUpdate(){
|
||||
Emp emp = new Emp();
|
||||
emp.setId(21);
|
||||
emp.setUsername("ls3");
|
||||
emp.setName("李四3");
|
||||
emp.setGender((short) 1);
|
||||
emp.setImage("1.jpg");
|
||||
emp.setJob((short) 1);
|
||||
emp.setEntrydate(LocalDate.of(2015,1,1));
|
||||
emp.setDeptId(1);
|
||||
emp.setUpdateTime(LocalDateTime.now());
|
||||
empMapper.update(emp);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testSelectById(){
|
||||
Emp emp = empMapper.selectById(14);
|
||||
System.out.println(emp);
|
||||
}
|
||||
|
||||
//条件查询测试
|
||||
@Test
|
||||
void testList(){
|
||||
List<Emp> list = empMapper.list("张", (short) 1, LocalDate.of(2010, 1, 1), LocalDate.of(2020, 1, 1));
|
||||
System.out.println(list);
|
||||
}
|
||||
|
||||
//条件查询测试
|
||||
@Test
|
||||
void testList1(){
|
||||
// List<Emp> list = empMapper.list("张", null, null,null);
|
||||
List<Emp> list = empMapper.list(null, (short)1, null,null);
|
||||
System.out.println(list);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdate1(){
|
||||
Emp emp = new Emp();
|
||||
emp.setId(20);
|
||||
emp.setGender((short)1);
|
||||
emp.setUsername("ls6");
|
||||
emp.setName("李四6");
|
||||
emp.setUpdateTime(LocalDateTime.now());
|
||||
empMapper.update(emp);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteByIds(){
|
||||
empMapper.deleteByIds(Arrays.asList(19,20,21));
|
||||
}
|
||||
}
|
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/
|
105
springboot-mybatis-quickstart/pom.xml
Normal file
105
springboot-mybatis-quickstart/pom.xml
Normal file
@@ -0,0 +1,105 @@
|
||||
<?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>-->
|
||||
|
||||
|
||||
<!--springboot单元测试依赖-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!--阿里巴巴的DRUID依赖-->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>1.2.8</version>
|
||||
</dependency>
|
||||
<!--lombok依赖-->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<!--统一springboot依赖管理版本-->
|
||||
<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,20 @@
|
||||
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;
|
||||
|
||||
//让mybatis进行识别是Mapper接口
|
||||
//注意:在运行时,@Mapper会自动生成该接口UserMapper的实现类对象(代理对象),并且将该代理对象交给spring的IOC容器管理
|
||||
@Mapper
|
||||
public interface UserMapper {
|
||||
|
||||
/*
|
||||
查询所有用户数据
|
||||
*/
|
||||
// @Select("select * from user;")
|
||||
@Select("select * from user")
|
||||
public List<User> list();
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
package com.inmind.pojo;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
/*@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@EqualsAndHashCode*/
|
||||
@Data
|
||||
@NoArgsConstructor //生成无参构造
|
||||
@AllArgsConstructor//生成满参构造方法
|
||||
public class User {
|
||||
private Integer id;
|
||||
private String name;
|
||||
private Integer age;
|
||||
private Short gender;
|
||||
private String phone;
|
||||
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
#下面这些内容是为了让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/mybatis
|
||||
#连接数据库的用户名
|
||||
spring.datasource.username=root
|
||||
#连接数据库的密码
|
||||
spring.datasource.password=1234
|
@@ -0,0 +1,66 @@
|
||||
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.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootTest //该注解的作用,启动spring环境,所以也就有了IOC容器
|
||||
class SpringbootMybatisQuickstartApplicationTests {
|
||||
|
||||
@Autowired//直接从IOC容器中获取UserMapper类型的对象
|
||||
private UserMapper userMapper;//接口开发,多态
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
//使用mybatis框架查询所有用户的数据
|
||||
List<User> list = userMapper.list();
|
||||
list.stream().forEach(user -> {
|
||||
System.out.println(user.getId());
|
||||
System.out.println(user);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void testJDBC() throws ClassNotFoundException, SQLException {
|
||||
//1. 注册驱动
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
|
||||
//2. 获取连接对象
|
||||
String url = "jdbc:mysql://localhost:3306/mybatis";
|
||||
String username = "root";
|
||||
String password = "1234";
|
||||
Connection connection = DriverManager.getConnection(url, username, password);
|
||||
|
||||
//3. 获取执行SQL的对象Statement,执行SQL,返回结果
|
||||
String sql = "select * from user";
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet resultSet = statement.executeQuery(sql);
|
||||
|
||||
//4. 封装结果数据
|
||||
List<User> userList = new ArrayList<>();
|
||||
while (resultSet.next()){
|
||||
int id = resultSet.getInt("id");
|
||||
String name = resultSet.getString("name");
|
||||
int age = resultSet.getInt("age");
|
||||
short gender = resultSet.getShort("gender");
|
||||
String phone = resultSet.getString("phone");
|
||||
|
||||
User user = new User(id,name,age,gender,phone);
|
||||
userList.add(user);
|
||||
}
|
||||
|
||||
//5. 释放资源
|
||||
statement.close();
|
||||
connection.close();
|
||||
|
||||
|
||||
userList.forEach(System.out::println);
|
||||
}
|
||||
|
||||
}
|
@@ -13,17 +13,26 @@
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<spring-boot.version>2.7.6</spring-boot.version>
|
||||
</properties>
|
||||
<!--依赖管理-->
|
||||
<dependencies>
|
||||
<!--web起步依赖-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--test起步依赖-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!--dom4j解析xml的依赖-->
|
||||
<dependency>
|
||||
<groupId>org.dom4j</groupId>
|
||||
<artifactId>dom4j</artifactId>
|
||||
<version>2.1.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
|
@@ -2,7 +2,9 @@ package com.inmind;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
//@ComponentScan({"dao","com.inmind"}) ,spring的4大组件注解必须要被扫描到才能生效,默认扫描启动项同一级类或子包中的类
|
||||
@SpringBootApplication
|
||||
public class SpringbootWebReqRespApplication {
|
||||
|
||||
|
@@ -0,0 +1,67 @@
|
||||
package com.inmind.controller;
|
||||
|
||||
import com.inmind.pojo.Emp;
|
||||
import com.inmind.pojo.Result;
|
||||
import com.inmind.service.EmpService;
|
||||
import com.inmind.service.impl.EmpServiceA;
|
||||
import com.inmind.utils.XmlParserUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
public class EmpController {
|
||||
|
||||
/* @Autowired//运行时,IOC容器会提高该类型的bean对象,并赋值(依赖注入)
|
||||
// @Qualifier("empServiceA")//注入时直接指定Bean对象的名称
|
||||
@Qualifier("empServiceB")//注入时直接指定Bean对象的名称
|
||||
private EmpService empService;*/
|
||||
|
||||
//方式三
|
||||
@Resource(name = "empServiceB")
|
||||
private EmpService empService;
|
||||
|
||||
/*@RequestMapping("/listEmp")
|
||||
public Result listEmp(){
|
||||
//1.加载并解析emp.xml
|
||||
// String file = "D:\\workspace_idea\\inmind_web_project250915\\springboot-web-req-resp\\src\\main\\resources\\emp.xml";
|
||||
//使用类加载器,获取本项目资源路径
|
||||
String file = this.getClass().getClassLoader().getResource("emp.xml").getFile();
|
||||
List<Emp> empList = XmlParserUtils.parse(file, Emp.class);
|
||||
//2.对数据进行转换
|
||||
empList.stream().forEach(emp->{
|
||||
//处理性别 gender: 1就是男 2就是女
|
||||
if (emp.getGender().equals("1")) {
|
||||
emp.setGender("男");
|
||||
} else {
|
||||
emp.setGender("女");
|
||||
}
|
||||
//处理职位 job : 1 - 讲师 2-班主任 3-辅导员
|
||||
String job = emp.getJob();
|
||||
if ("1".equals( job)) {
|
||||
emp.setJob("讲师");
|
||||
} else if ("2".equals(job)) {
|
||||
emp.setJob("班主任");
|
||||
} else {
|
||||
emp.setJob("辅导员");
|
||||
}
|
||||
});
|
||||
//3.响应数据
|
||||
|
||||
return Result.success(empList);
|
||||
}*/
|
||||
|
||||
|
||||
@RequestMapping("/listEmp")
|
||||
public Result listEmp(){
|
||||
//调用业务层的获取员工的方法
|
||||
List<Emp> empList = empService.listEmp();
|
||||
//响应数据
|
||||
return Result.success(empList);
|
||||
}
|
||||
}
|
@@ -2,9 +2,7 @@ package com.inmind.controller;
|
||||
|
||||
import com.inmind.pojo.User;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.time.LocalDateTime;
|
||||
@@ -83,4 +81,19 @@ public class RequestController {
|
||||
System.out.println(updateTime.getDayOfMonth());
|
||||
return "ok";
|
||||
}
|
||||
|
||||
|
||||
//接收json请求数据
|
||||
@RequestMapping("/jsonParam")
|
||||
public String jsonParam(@RequestBody User user){
|
||||
System.out.println(user);
|
||||
return "OK";
|
||||
}
|
||||
|
||||
//路径参数(REST风格)
|
||||
@RequestMapping("/path/{id}/{name}")
|
||||
public String pathParam(@PathVariable Integer id,@PathVariable String name) {
|
||||
System.out.println("id:"+id+"name:"+name);
|
||||
return "OK";
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,82 @@
|
||||
package com.inmind.controller;
|
||||
|
||||
import com.inmind.pojo.Address;
|
||||
import com.inmind.pojo.Result;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RestController//RestController中包含@ResponseBody,如果在类上添加了@ResponseBody,那就代表类中所有方法上都设置了@ResponseBody
|
||||
public class ResponseController {
|
||||
|
||||
/*//响应普通的字符串
|
||||
@RequestMapping("/hello")
|
||||
public String hello(){
|
||||
System.out.println("hello World");
|
||||
return "hello World";
|
||||
}
|
||||
|
||||
//响应对象
|
||||
@RequestMapping("/getAddr")
|
||||
public Address getAddr(){
|
||||
Address address = new Address();
|
||||
address.setProvince("江苏");
|
||||
address.setCity("常州");
|
||||
return address;
|
||||
}
|
||||
|
||||
//响应集合
|
||||
@RequestMapping("/listAddr")
|
||||
public List<Address> listAddr(){
|
||||
ArrayList<Address> list = new ArrayList<>();
|
||||
|
||||
Address address = new Address();
|
||||
address.setProvince("江苏");
|
||||
address.setCity("常州");
|
||||
|
||||
list.add(address);
|
||||
Address address1 = new Address();
|
||||
address1.setProvince("江苏");
|
||||
address1.setCity("苏州");
|
||||
|
||||
list.add(address1);
|
||||
return list;
|
||||
}*/
|
||||
|
||||
//响应普通的字符串
|
||||
@RequestMapping("/hello")
|
||||
public Result hello(){
|
||||
System.out.println("hello World");
|
||||
return Result.success("hello world");
|
||||
}
|
||||
|
||||
//响应对象
|
||||
@RequestMapping("/getAddr")
|
||||
public Result getAddr(){
|
||||
Address address = new Address();
|
||||
address.setProvince("江苏");
|
||||
address.setCity("常州");
|
||||
return Result.success(address);
|
||||
}
|
||||
|
||||
//响应集合
|
||||
@RequestMapping("/listAddr")
|
||||
public Result listAddr(){
|
||||
ArrayList<Address> list = new ArrayList<>();
|
||||
|
||||
Address address = new Address();
|
||||
address.setProvince("江苏");
|
||||
address.setCity("常州");
|
||||
|
||||
list.add(address);
|
||||
Address address1 = new Address();
|
||||
address1.setProvince("江苏");
|
||||
address1.setCity("苏州");
|
||||
|
||||
list.add(address1);
|
||||
return Result.success(list);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,12 @@
|
||||
package com.inmind.dao;
|
||||
|
||||
import com.inmind.pojo.Emp;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface EmpDao {
|
||||
/*
|
||||
获取员工数据
|
||||
*/
|
||||
public List<Emp> listEmp();
|
||||
}
|
@@ -0,0 +1,20 @@
|
||||
package com.inmind.dao.impl;
|
||||
|
||||
import com.inmind.dao.EmpDao;
|
||||
import com.inmind.pojo.Emp;
|
||||
import com.inmind.utils.XmlParserUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
//从XML文件中获取数据
|
||||
@Component//交给IOC容器管理
|
||||
//@Repository
|
||||
public class EmpDaoA implements EmpDao {
|
||||
@Override
|
||||
public List<Emp> listEmp() {
|
||||
//1.加载并解析emp.xml,使用类加载器,获取本项目资源路径
|
||||
String file = this.getClass().getClassLoader().getResource("emp.xml").getFile();
|
||||
List<Emp> empList = XmlParserUtils.parse(file, Emp.class);
|
||||
return empList;
|
||||
}
|
||||
}
|
@@ -0,0 +1,71 @@
|
||||
package com.inmind.pojo;
|
||||
|
||||
public class Emp {
|
||||
private String name;
|
||||
private Integer age;
|
||||
private String image;
|
||||
private String gender;
|
||||
private String job;
|
||||
|
||||
public Emp() {
|
||||
}
|
||||
|
||||
public Emp(String name, Integer age, String image, String gender, String job) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.image = image;
|
||||
this.gender = gender;
|
||||
this.job = job;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public void setImage(String image) {
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
public String getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public void setGender(String gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
public String getJob() {
|
||||
return job;
|
||||
}
|
||||
|
||||
public void setJob(String job) {
|
||||
this.job = job;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Emp{" +
|
||||
"name='" + name + '\'' +
|
||||
", age=" + age +
|
||||
", image='" + image + '\'' +
|
||||
", gender='" + gender + '\'' +
|
||||
", job='" + job + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@@ -0,0 +1,66 @@
|
||||
package com.inmind.pojo;
|
||||
|
||||
public class Result {
|
||||
private Integer code;//1:成功 0:失败
|
||||
private String msg;//提示信息
|
||||
private Object data;//数据data
|
||||
|
||||
public Result() {
|
||||
}
|
||||
|
||||
public Result(Integer code, String msg, Object data) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(Integer code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public void setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public Object getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(Object data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Result{" +
|
||||
"code=" + code +
|
||||
", msg='" + msg + '\'' +
|
||||
", data=" + data +
|
||||
'}';
|
||||
}
|
||||
|
||||
//封装一些固定的成功和失败的方法,供外部调用
|
||||
public static Result success(){
|
||||
/*Result result = new Result(1, "成功", null);
|
||||
return result;*/
|
||||
return success(null);
|
||||
}
|
||||
|
||||
public static Result success(Object data){
|
||||
Result result = new Result(1, "成功", data);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Result error(String msg){
|
||||
Result result = new Result(0, msg, null);
|
||||
return result;
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
package com.inmind.service;
|
||||
|
||||
import com.inmind.pojo.Emp;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
//员工相关业务层
|
||||
public interface EmpService {
|
||||
//获取员工列表
|
||||
public List<Emp> listEmp();
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
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.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
//@Component//将当前类交给IOC容器管理,称为IOC容器中的bean对象(IOC控制反转)
|
||||
//@Primary
|
||||
@Service
|
||||
public class EmpServiceA implements EmpService {
|
||||
|
||||
@Autowired //IOC容器进行依赖注入赋值
|
||||
private EmpDao empDao;
|
||||
/*
|
||||
获取到数据之后,对员工数据加以处理
|
||||
*/
|
||||
@Override
|
||||
public List<Emp> listEmp() {
|
||||
//1.从dao层获取数据
|
||||
List<Emp> empList = empDao.listEmp();
|
||||
//2.对数据进行转换
|
||||
empList.stream().forEach(emp->{
|
||||
//处理性别 gender: 1就是男 2就是女
|
||||
if (emp.getGender().equals("1")) {
|
||||
emp.setGender("男");
|
||||
} else {
|
||||
emp.setGender("女");
|
||||
}
|
||||
//处理职位 job : 1 - 讲师 2-班主任 3-辅导员
|
||||
String job = emp.getJob();
|
||||
if ("1".equals( job)) {
|
||||
emp.setJob("讲师");
|
||||
} else if ("2".equals(job)) {
|
||||
emp.setJob("班主任");
|
||||
} else {
|
||||
emp.setJob("辅导员");
|
||||
}
|
||||
});
|
||||
return empList;
|
||||
}
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
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对象(IOC控制反转)
|
||||
//@Primary
|
||||
@Service
|
||||
public class EmpServiceB implements EmpService {
|
||||
|
||||
@Autowired //IOC容器进行依赖注入赋值
|
||||
private EmpDao empDao;
|
||||
/*
|
||||
获取到数据之后,对员工数据加以处理
|
||||
*/
|
||||
@Override
|
||||
public List<Emp> listEmp() {
|
||||
//1.从dao层获取数据
|
||||
List<Emp> empList = empDao.listEmp();
|
||||
//2.对数据进行转换
|
||||
empList.stream().forEach(emp->{
|
||||
//处理性别 gender: 1就是男 2就是女
|
||||
if (emp.getGender().equals("1")) {
|
||||
emp.setGender("男士");
|
||||
} else {
|
||||
emp.setGender("女士");
|
||||
}
|
||||
//处理职位 job : 1 - 讲师 2-班主任 3-辅导员
|
||||
String job = emp.getJob();
|
||||
if ("1".equals( job)) {
|
||||
emp.setJob("讲师1");
|
||||
} else if ("2".equals(job)) {
|
||||
emp.setJob("班主任1");
|
||||
} else {
|
||||
emp.setJob("辅导员");
|
||||
}
|
||||
});
|
||||
return empList;
|
||||
}
|
||||
}
|
@@ -0,0 +1,54 @@
|
||||
package com.inmind.utils;
|
||||
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.Element;
|
||||
import org.dom4j.io.SAXReader;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class XmlParserUtils {
|
||||
|
||||
public static <T> List<T> parse(String file , Class<T> targetClass) {
|
||||
ArrayList<T> list = new ArrayList<T>(); //封装解析出来的数据
|
||||
try {
|
||||
//1.获取一个解析器对象
|
||||
SAXReader saxReader = new SAXReader();
|
||||
//2.利用解析器把xml文件加载到内存中,并返回一个文档对象
|
||||
Document document = saxReader.read(new File(file));
|
||||
//3.获取到根标签
|
||||
Element rootElement = document.getRootElement();
|
||||
//4.通过根标签来获取 user 标签
|
||||
List<Element> elements = rootElement.elements("emp");
|
||||
|
||||
//5.遍历集合,得到每一个 user 标签
|
||||
for (Element element : elements) {
|
||||
//获取 name 属性
|
||||
String name = element.element("name").getText();
|
||||
//获取 age 属性
|
||||
String age = element.element("age").getText();
|
||||
//获取 image 属性
|
||||
String image = element.element("image").getText();
|
||||
//获取 gender 属性
|
||||
String gender = element.element("gender").getText();
|
||||
//获取 job 属性
|
||||
String job = element.element("job").getText();
|
||||
|
||||
//组装数据
|
||||
Constructor<T> constructor = targetClass.getDeclaredConstructor(String.class, Integer.class, String.class, String.class, String.class);
|
||||
constructor.setAccessible(true);
|
||||
T object = constructor.newInstance(name, Integer.parseInt(age), image, gender, job);
|
||||
|
||||
list.add(object);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
36
springboot-web-req-resp/src/main/resources/emp.xml
Normal file
36
springboot-web-req-resp/src/main/resources/emp.xml
Normal file
@@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<emps>
|
||||
<emp>
|
||||
<name>金毛狮王</name>
|
||||
<age>55</age>
|
||||
<image>https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/1.jpg</image>
|
||||
<!-- 1: 男, 2: 女 -->
|
||||
<gender>1</gender>
|
||||
<!-- 1: 讲师, 2: 班主任 , 3: 就业指导 -->
|
||||
<job>1</job>
|
||||
</emp>
|
||||
|
||||
<emp>
|
||||
<name>白眉鹰王</name>
|
||||
<age>65</age>
|
||||
<image>https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/2.jpg</image>
|
||||
<gender>1</gender>
|
||||
<job>1</job>
|
||||
</emp>
|
||||
|
||||
<emp>
|
||||
<name>青翼蝠王</name>
|
||||
<age>45</age>
|
||||
<image>https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/3.jpg</image>
|
||||
<gender>1</gender>
|
||||
<job>2</job>
|
||||
</emp>
|
||||
|
||||
<emp>
|
||||
<name>紫衫龙王</name>
|
||||
<age>38</age>
|
||||
<image>https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/4.jpg</image>
|
||||
<gender>2</gender>
|
||||
<job>3</job>
|
||||
</emp>
|
||||
</emps>
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
60
springboot-web-req-resp/src/main/resources/static/emp.html
Normal file
60
springboot-web-req-resp/src/main/resources/static/emp.html
Normal file
@@ -0,0 +1,60 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>员工信息</title>
|
||||
</head>
|
||||
|
||||
<link rel="stylesheet" href="element-ui/index.css">
|
||||
<script src="./js/vue.js"></script>
|
||||
<script src="./element-ui/index.js"></script>
|
||||
<script src="./js/axios-0.18.0.js"></script>
|
||||
|
||||
<body>
|
||||
<h1 align="center">员工信息列表展示</h1>
|
||||
<div id="app">
|
||||
<el-table :data="tableData" style="width: 100%" stripe border >
|
||||
<el-table-column prop="name" label="姓名" align="center" min-width="20%"></el-table-column>
|
||||
<el-table-column prop="age" label="年龄" align="center" min-width="20%"></el-table-column>
|
||||
<el-table-column label="图像" align="center" min-width="20%">
|
||||
<template slot-scope="scope">
|
||||
<el-image :src="scope.row.image" style="width: 80px; height: 50px;"></el-image>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="gender" label="性别" align="center" min-width="20%"></el-table-column>
|
||||
<el-table-column prop="job" label="职位" align="center" min-width="20%"></el-table-column>
|
||||
</el-table>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
<style>
|
||||
.el-table .warning-row {
|
||||
background: oldlace;
|
||||
}
|
||||
.el-table .success-row {
|
||||
background: #f0f9eb;
|
||||
}
|
||||
</style>
|
||||
|
||||
<script>
|
||||
new Vue({
|
||||
el: "#app",
|
||||
data() {
|
||||
return {
|
||||
tableData: []
|
||||
}
|
||||
},
|
||||
mounted(){
|
||||
axios.get('/listEmp').then(res=>{
|
||||
if(res.data.code){
|
||||
this.tableData = res.data.data;
|
||||
}
|
||||
});
|
||||
},
|
||||
methods: {
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</html>
|
File diff suppressed because one or more lines are too long
11944
springboot-web-req-resp/src/main/resources/static/js/vue.js
Normal file
11944
springboot-web-req-resp/src/main/resources/static/js/vue.js
Normal file
File diff suppressed because it is too large
Load Diff
33
tlias-web-management/.gitignore
vendored
Normal file
33
tlias-web-management/.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/
|
93
tlias-web-management/pom.xml
Normal file
93
tlias-web-management/pom.xml
Normal file
@@ -0,0 +1,93 @@
|
||||
<?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>tlias-web-management</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>tlias-web-management</name>
|
||||
<description>tlias-web-management</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>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</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>
|
||||
<!--作用:在maven编译时,保留源文件.java中的方法参数名-->
|
||||
<compilerArgs>
|
||||
<arg>-parameters</arg>
|
||||
</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<configuration>
|
||||
<mainClass>com.inmind.TliasWebManagementApplication</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 TliasWebManagementApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TliasWebManagementApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
package com.inmind.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class DeptController {
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
package com.inmind.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class EmpController {
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
package com.inmind.mapper;
|
||||
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface DeptMapper {
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
package com.inmind.mapper;
|
||||
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface EmpMapper {
|
||||
}
|
19
tlias-web-management/src/main/java/com/inmind/pojo/Dept.java
Normal file
19
tlias-web-management/src/main/java/com/inmind/pojo/Dept.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.inmind.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 部门实体类
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Dept {
|
||||
private Integer id; //ID
|
||||
private String name; //部门名称
|
||||
private LocalDateTime createTime; //创建时间
|
||||
private LocalDateTime updateTime; //修改时间
|
||||
}
|
27
tlias-web-management/src/main/java/com/inmind/pojo/Emp.java
Normal file
27
tlias-web-management/src/main/java/com/inmind/pojo/Emp.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package com.inmind.pojo;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 员工实体类
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Emp {
|
||||
private Integer id; //ID
|
||||
private String username; //用户名
|
||||
private String password; //密码
|
||||
private String name; //姓名
|
||||
private Short gender; //性别 , 1 男, 2 女
|
||||
private String image; //图像url
|
||||
private Short job; //职位 , 1 班主任 , 2 讲师 , 3 学工主管 , 4 教研主管 , 5 咨询师
|
||||
private LocalDate entrydate; //入职日期
|
||||
private Integer deptId; //部门ID
|
||||
private LocalDateTime createTime; //创建时间
|
||||
private LocalDateTime updateTime; //修改时间
|
||||
}
|
@@ -0,0 +1,4 @@
|
||||
package com.inmind.service;
|
||||
|
||||
public interface DeptService {
|
||||
}
|
@@ -0,0 +1,4 @@
|
||||
package com.inmind.service;
|
||||
|
||||
public interface EmpService {
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
package com.inmind.service.impl;
|
||||
|
||||
import com.inmind.service.DeptService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class DeptServiceImpl implements DeptService {
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
package com.inmind.service.impl;
|
||||
|
||||
import com.inmind.service.EmpService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class EmpServiceImpl implements EmpService {
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
# 应用服务 WEB 访问端口
|
||||
server.port=8080
|
||||
#下面这些内容是为了让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/tlias1
|
||||
#连接数据库的用户名
|
||||
spring.datasource.username=root
|
||||
#连接数据库的密码
|
||||
spring.datasource.password=1234
|
||||
|
||||
#输出mybatis的日志
|
||||
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
|
||||
#开启mybatis的驼峰命名自动映射开关,将a_time ---->aTime
|
||||
mybatis.configuration.map-underscore-to-camel-case=true
|
@@ -0,0 +1,6 @@
|
||||
<html>
|
||||
<body>
|
||||
<h1>hello word!!!</h1>
|
||||
<p>this is a html page</p>
|
||||
</body>
|
||||
</html>
|
@@ -0,0 +1,13 @@
|
||||
package com.inmind;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class TliasWebManagementApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user