在pom.xml添加依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
在application.properties添加
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/girls
spring.datasource.username=root
spring.datasource.password=chendashan
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
server.port=8080
server.session.timeout=10
server.tomcat.uri-encoding=UTF-8
mybatis.configLocations= classpath:mybatis-config.xml
mybatis.mapper-locations=classpath:mapper/*.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">
<!-- namespace用于绑定Dao接口 -->
<mapper namespace="com.housekeeper.dao.UserDao">
<!-- 用用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.housekeeper.model.User" >
<!-- column代表数据库列名,property代表实体类属性名 -->
<result column="user_id" property="userId"/>
<result column="user_name" property="userName"/>
<result column="user_password" property="userPassword"/>
</resultMap>
<!-- 查询名字记录sql -->
<select id="selectUserByUserName" parameterType="String" resultMap="BaseResultMap">
SELECT * FROM girls_info WHERE user_name = #{userName}
</select>
</mapper>
综上得知,UserDao通过映射文件mapper,执行了sql语句,返回了实体类User
UserDao接口
@Mapper
public interface UserDao {
/**
* 根据user_name查询数据库
* (映射执行mapper文件中的sql语句selectUserByUserName)
* @param userName 名字
* @return User
*/
public User selectUserByUserName(String userName);
}
User实体类
public class User {
private String userName;
private String userPassword;
public String getUserName() {
return userName;
}
public void setName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setPassword(String userPassword) {
this.userPassword = userPassword;
}
}
逻辑层在controller里处理,已知,执行Userdao的接口方法,即可操作数据库。为了更好处理逻辑分层,加入Service层,调用UserDao。在Service实现层,注入UserDao即可调用其方法。
@Service
public class UserServiceImp implements UserService {
@Autowired
private UserDao userDao;//注入UserDao
@Override
public User selectUserByName(String userName) {
return userDao.selectUserByUserName(userName);
}
}
public interface UserService {
/**
* 通过姓名查找User
* @param userName
* @return
*/
User selectUserByName(String useName);
}
最后,controller层对外提供接口,返回查询数据结果
@Controller
public class UserController {
@Autowired
private UserService userService;//注入Service
@ResponseBody
@RequestMapping(value = "/login", method = RequestMethod.POST)
public Map<String, Object> login(@RequestParam(value = "userName", required = true) String userName,
@RequestParam(value = "userPassword", required = true) String userPassword) {
Map<String,Object> result = new HashMap<String, Object>();
User user = null;
String retCode = "";
String retMsg = "";
if(StringUtils.isEmpty(userName) || StringUtils.isEmpty(userPassword)){
retCode = "01";
retMsg = "用户名和密码不能为空";
}else{
user = userService.selectUserByName(userName);
if(null == user){
retCode = "01";
retMsg = "用户不存在";
}else{
if(userPassword.equals(user.getUserPassword())){
retCode = "00";
retMsg = "登录成功";
}else{
retCode = "01";
retMsg = "密码有误";
}
}
}
result.put(SystemConst.retCode, retCode);
result.put(SystemConst.retMsg, retMsg);
return result;
}
}
girls.sql文件
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `girls_info`
-- ----------------------------
DROP TABLE IF EXISTS `girls_info`;
CREATE TABLE `girls_info` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(30) NOT NULL,
`user_password` varchar(10) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of girls_info
-- ----------------------------
INSERT INTO `girls_info` VALUES ('2', '李一', '123456');
INSERT INTO `girls_info` VALUES ('3', '陈二', '123456');
INSERT INTO `girls_info` VALUES ('4', '王三', '123456');
INSERT INTO `girls_info` VALUES ('5', '张四', '123456');
INSERT INTO `girls_info` VALUES ('6', '李五', '123456');
INSERT INTO `girls_info` VALUES ('7', 'Biligle', '123456');
动力节点在线课程涵盖零基础入门,高级进阶,在职提升三大主力内容,覆盖Java从入门到就业提升的全体系学习内容。全部Java视频教程免费观看,相关学习资料免费下载!对于火爆技术,每周一定时更新!如果想了解更多相关技术,可以到动力节点在线免费观看SpringBoot基础教程哦!
代码小兵87208-09 15:04
代码小兵99203-29 17:29
杨晶珍08-02 11:09
代码小兵98808-09 14:38
代码小兵28608-09 14:50