1.首先在数据库里面新建一张表customer,表的设计如下:
然后在表中插入几条数据。
2. 整个项目的结构如下所示:
3.在application.properties中配置mysql,以及mybatis的映射路径,这里的url的数据库名记得改成自己的哦。注意:
这里的mybatis映射路径的classpath*与classpath的区别,WEB-INF/classes,lib才是classpath,classpath* 会查找根目录下所有的文件,直到找到为止。
#mysql配置
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf8&useSSL=\
false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis映射路径
mybatis.mapper-locations:classpath*:userMapper.xml
4.model包新建实体类User,这里的@Setter,@Getter,是添加lombok的插件依赖
@Setter
@Getter
public class User {
private String userId;
private String userName;
private String phone;
private Date createDate;
private Date updateDate;
}
5.service包下新建接口UserService,根据用户Id获取用户信息
public interface UserService {
User getUserName(String userId);
}
6.dao包下新建接口UserDao
@Mapper
public interface UserDao {
User getUserName(String userId);
}
7.serviceImpl包下新建实现类UserServiceImpl
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public User getUserName(String userId) {
return userDao.getUserName(userId);
}
}
8.UserMapper.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.example.springbootdemo.dao.UserDao">
<resultMap id="BaseResultMap" type="com.example.springbootdemo.model.User">
<id column="userId" jdbcType="VARCHAR" property="userId" />
<result column="userName" jdbcType="VARCHAR" property="userName" />
<result column="phone" jdbcType="VARCHAR" property="phone" />
<result column="createDate" jdbcType="TIMESTAMP" property="createDate" />
<result column="updateDate" jdbcType="TIMESTAMP" property="updateDate" />
</resultMap>
<select id="getUserName" resultMap="BaseResultMap">
select * from customer where userId = #{userId}
</select>
</mapper>
9.UserController类的代码如下:
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/userName")
public User getUserName(String userId){
return userService.getUserName(userId);
}
}
10.启动项目,在浏览器里面输入:http://127.0.0.1:8080/userName?userId=1,即可得到用户id=1的信息。如下图所示:
就此,一个简单的雏形就搭建成功啦。
下面,简单的实现一个操作:根据id获取用户信息的时候,先从redis里面获取数据,如果redis里面没有数据,则从mysql数据库中读取数据,由于本地操作没有数据,所以我先将数据库中的数据插入redis.实现类的代码修改为如下:
启动前,记得要将redis的服务运行哦。
启动之后,浏览器中输入http://127.0.0.1:8080/userName?userId=1,发现报如下的错误:
2020-02-16 07:40:57.282 ERROR 13532 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.redis.serializer.SerializationException: Cannot serialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to serialize object using DefaultSerializer; nested exception is java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type [com.example.springbootdemo.model.User]] with root cause
java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type [com.example.springbootdemo.model.User]
at org.springframework.core.serializer.DefaultSerializer.serialize(DefaultSerializer.java:43) ~[spring-core-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.core.serializer.support.SerializingConverter.convert(SerializingConverter.java:63) ~[spring-core-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.core.serializer.support.SerializingConverter.convert(SerializingConverter.java:35) ~[spring-core-5.2.3.RELEASE.jar:5.2.3.RELEASE]
at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.serialize(JdkSerializationRedisSerializer.java:94) ~[spring-data-redis-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.data.redis.core.AbstractOperations.rawValue(AbstractOperations.java:127) ~[spring-data-redis-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:235) ~[spring-data-redis-2.2.4.RELEASE.jar:2.2.4.RELEASE]
at com.example.springbootdemo.serviceImpl.UserServiceImpl.getUserName(UserServiceImpl.java:24) ~[classes/:na]
at com.example.springbootdemo.controller.UserController.getUserName(UserController.java:23) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.3.RELEASE.jar:5.2.3.RELEASE]
原因是:缓存时,spring会将对象先序列化再存入redis,所以我们要将实体类对象实现Serializable。
最后,再运行一遍,成功!