IDEA中新建SpringBoot项目
可能遇到问题:Unable to import maven project,解决方法为:File>Settings,切到Maven项,修改以下三处。
其中,settings.xml的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>D:\.m2\repository</localRepository>
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
</servers>
<mirrors>
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central</url>
</mirror>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>central</id>
<name>Maven Repository Switchboard</name>
<url>http://repo1.maven.org/maven2</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2</url>
</mirror>
<mirror>
<id>ibiblio</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://mirrors.ibiblio.org/pub/mirrors/maven2</url>
</mirror>
<mirror>
<id>jboss-public-repository-group</id>
<mirrorOf>central</mirrorOf>
<name>JBoss Public Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public</url>
</mirror>
<mirror>
<id>google-maven-central</id>
<name>Google Maven Central</name>
<url>https://maven-central.storage.googleapis.com
</url>
<mirrorOf>central</mirrorOf>
</mirror>
<!-- 中央仓库在中国的镜像 -->
<mirror>
<id>maven.net.cn</id>
<name>one of the central mirrors in china</name>
<url>http://maven.net.cn/content/groups/public</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<profiles>
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
<profile>
<id>repository_set</id>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>public</id>
<name>Public Repository</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>public</id>
<name>Public Repository</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
</settings>
定义一个接口
新建目录controller,并在该目录下新建类TestController
,如下所示,
package com.jepcc.test.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("/test")
public String test(){
return "Hello,World!";
}
}
运行TestApplication.main()
后,浏览器访问地址http://localhost:8080/test
,返回字符串"Hello,World!"。
Navicate中新建数据库、表及专有账户
数据库名 | test-database |
表名 | list |
专有用户名及密码 | test-user/test@123 |
IDEA里操作数据库
由于社区版IDEA没有Database,所以安装插件Database Navigator,如下图所示,更多内容可以参考这篇文章。
集成MyBatis
- pom.xml中添加MyBatis、Mysql连接的依赖
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jepcc</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test</name>
<description>test for spring boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 集成mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!-- 集成mysql连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- application.properties中添加Mysql连接信息、Mapper层xml文件所在位置
spring.datasource.url=jdbc:mysql://localhost:3306/test-database
spring.datasource.username=test-user
spring.datasource.password=test@123
spring.datasource.driver-class=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:/mapper/**/*.xml
- TestMapper.xml,Mapper层要执行的sql语句就在这个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.jepcc.test.mapper.TestMapper">
<select id="list" resultType="com.jepcc.test.model.Ebook">
select `id`,`name`,`passwd` from `list`
</select>
</mapper>
- controller层,com.jepcc.test.controller.TestController
package com.jepcc.test.controller;
import com.jepcc.test.model.Ebook;
import com.jepcc.test.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class TestController {
@Autowired
public TestService testService;
@GetMapping("/test/list")
public List<Ebook> list(){
return testService.list();
}
}
- service层,com.jepcc.test.service.TestService
package com.jepcc.test.service;
import com.jepcc.test.mapper.TestMapper;
import com.jepcc.test.model.Ebook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class TestService {
@Autowired
public TestMapper testMapper;
public List<Ebook> list(){
return testMapper.list();
}
}
- mapper层,com.jepcc.test.mapper.TestMapper
package com.jepcc.test.mapper;
import com.jepcc.test.model.Ebook;
import java.util.List;
public interface TestMapper {
public List<Ebook> list();
}
- model层,com.jepcc.test.model.Ebook
package com.jepcc.test.mapper;
import com.jepcc.test.model.Ebook;
import java.util.List;
public interface TestMapper {
public List<Ebook> list();
}
- 启动类或主函数,com.jepcc.test.TestApplication
package com.jepcc.test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.jepcc.test.mapper")
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
集成Mybatis Generator
- Navicate中新建表users
- pom.xml添加Mybatis Generator、Java连接Mysql的驱动包
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jepcc</groupId>
<artifactId>wiki</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>wiki</name>
<description>Jepcc Wiki</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-devtools</artifactId>
</dependency>
<!-- 集成mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!-- 集成mysql连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.4.4</version>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<configurationFile>src/main/resources/generator/generator-config.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="Mysql" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test-database"
userId="test-user"
password="test@123">
</jdbcConnection>
<javaModelGenerator targetPackage="com.jepcc.test.model" targetProject="src\main\java">
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper" targetProject="src\main\resources">
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.jepcc.test.mapper" targetProject="src\main\java">
</javaClientGenerator>
<table tableName="users" domainObjectName="User" >
</table>
</context>
</generatorConfiguration>
- 添加并运行命令
mybatis-generator:generate -e
,生成相应的Model实体类User
、Mapper接口文件UserMapper
和Mapper配置文件UserMapper.xml
package com.jepcc.test.model;
public class User {
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column users.id
*
* @mbg.generated Thu May 06 15:12:26 CST 2021
*/
private Long id;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column users.name
*
* @mbg.generated Thu May 06 15:12:26 CST 2021
*/
private String name;
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column users.id
*
* @return the value of users.id
*
* @mbg.generated Thu May 06 15:12:26 CST 2021
*/
public Long getId() {
return id;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column users.id
*
* @param id the value for users.id
*
* @mbg.generated Thu May 06 15:12:26 CST 2021
*/
public void setId(Long id) {
this.id = id;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column users.name
*
* @return the value of users.name
*
* @mbg.generated Thu May 06 15:12:26 CST 2021
*/
public String getName() {
return name;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column users.name
*
* @param name the value for users.name
*
* @mbg.generated Thu May 06 15:12:26 CST 2021
*/
public void setName(String name) {
this.name = name;
}
}
package com.jepcc.test.mapper;
import com.jepcc.test.model.User;
import com.jepcc.test.model.UserExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface UserMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Thu May 06 15:12:26 CST 2021
*/
long countByExample(UserExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Thu May 06 15:12:26 CST 2021
*/
int deleteByExample(UserExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Thu May 06 15:12:26 CST 2021
*/
int deleteByPrimaryKey(Long id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Thu May 06 15:12:26 CST 2021
*/
int insert(User record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Thu May 06 15:12:26 CST 2021
*/
int insertSelective(User record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Thu May 06 15:12:26 CST 2021
*/
List<User> selectByExample(UserExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Thu May 06 15:12:26 CST 2021
*/
User selectByPrimaryKey(Long id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Thu May 06 15:12:26 CST 2021
*/
int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Thu May 06 15:12:26 CST 2021
*/
int updateByExample(@Param("record") User record, @Param("example") UserExample example);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Thu May 06 15:12:26 CST 2021
*/
int updateByPrimaryKeySelective(User record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table users
*
* @mbg.generated Thu May 06 15:12:26 CST 2021
*/
int updateByPrimaryKey(User record);
}
<?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.jepcc.test.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.jepcc.test.model.User">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu May 06 15:12:27 CST 2021.
-->
<id column="id" jdbcType="BIGINT" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
</resultMap>
<sql id="Example_Where_Clause">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu May 06 15:12:27 CST 2021.
-->
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu May 06 15:12:27 CST 2021.
-->
<where>
<foreach collection="example.oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu May 06 15:12:27 CST 2021.
-->
id, name
</sql>
<select id="selectByExample" parameterType="com.jepcc.test.model.UserExample" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu May 06 15:12:27 CST 2021.
-->
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from users
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu May 06 15:12:27 CST 2021.
-->
select
<include refid="Base_Column_List" />
from users
where id = #{id,jdbcType=BIGINT}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu May 06 15:12:27 CST 2021.
-->
delete from users
where id = #{id,jdbcType=BIGINT}
</delete>
<delete id="deleteByExample" parameterType="com.jepcc.test.model.UserExample">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu May 06 15:12:27 CST 2021.
-->
delete from users
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="com.jepcc.test.model.User">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu May 06 15:12:27 CST 2021.
-->
insert into users (id, name)
values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.jepcc.test.model.User">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu May 06 15:12:27 CST 2021.
-->
insert into users
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=BIGINT},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="com.jepcc.test.model.UserExample" resultType="java.lang.Long">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu May 06 15:12:27 CST 2021.
-->
select count(*) from users
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu May 06 15:12:27 CST 2021.
-->
update users
<set>
<if test="record.id != null">
id = #{record.id,jdbcType=BIGINT},
</if>
<if test="record.name != null">
name = #{record.name,jdbcType=VARCHAR},
</if>
</set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu May 06 15:12:27 CST 2021.
-->
update users
set id = #{record.id,jdbcType=BIGINT},
name = #{record.name,jdbcType=VARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByPrimaryKeySelective" parameterType="com.jepcc.test.model.User">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu May 06 15:12:27 CST 2021.
-->
update users
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
<update id="updateByPrimaryKey" parameterType="com.jepcc.test.model.User">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Thu May 06 15:12:27 CST 2021.
-->
update users
set name = #{name,jdbcType=VARCHAR}
where id = #{id,jdbcType=BIGINT}
</update>
</mapper>
- 添加controller层
UserController
和service层UserService
package com.jepcc.test.controller;
import com.jepcc.test.model.User;
import com.jepcc.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/test")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/user")
public List<User> getInfo(){
return userService.getInfo();
}
}
package com.jepcc.test.service;
import com.jepcc.test.mapper.TestMapper;
import com.jepcc.test.model.Ebook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class TestService {
@Autowired
public TestMapper testMapper;
public List<Ebook> list(){
return testMapper.list();
}
}
- 启动主程序,调用接口
使用泛型
Java5引入参数化类型的概念,也称为泛型。
泛型,允许定义类、接口、方法时使用类型形参,在声明变量、创建对象、调用方法时传入实际的类型参数,即类型实参。
本例中,TestController
使用了List<Ebook>
,UserController
使用了List<User>
,所以我们使用泛型来尝试下。
- 新建包
resp
,在该包下新建泛型类CommonResp
package com.jepcc.test.resp;
public class CommonResp<T> {
private Boolean success = true;
private String message;
private T content;
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getContent() {
return content;
}
public void setContent(T content) {
this.content = content;
}
}
- 在
TestController
和UserController
中使用泛型类
package com.jepcc.test.controller;
import com.jepcc.test.resp.CommonResp;
import com.jepcc.test.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
public TestService testService;
@GetMapping("/test/list")
public CommonResp list(){
CommonResp resp = new CommonResp();
resp.setContent(testService.list());
return resp;
}
}
package com.jepcc.test.controller;
import com.jepcc.test.resp.CommonResp;
import com.jepcc.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/user")
public CommonResp getInfo(){
CommonResp resp = new CommonResp();
resp.setContent(userService.getInfo());
return resp;
}
}
- 接口测试