动力节点首页 全国咨询热线:400-8080-105

绑定手机号,登录
手机号

验证码

微信登录
手机号登录
手机号

验证码

30天自动登录
微信登录与注册
微信扫码登录与注册

扫码关注微信公众号完成登录与注册
手机号登录
首页 > 文章

SpringBoot配置Mysql数据源

08-02 11:56 242浏览
举报 T字号
  • 大字
  • 中字
  • 小字

配置Mysql数据库

在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

建立库表省略

mapper文件

操作数据库,靠它完成。

<?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层对外提供接口,返回查询数据结果

@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基础教程哦!

0人推荐
共同学习,写下你的评论
0条评论
代码小兵696
程序员代码小兵696

118篇文章贡献392976字

作者相关文章更多>

推荐相关文章更多>

SpringBoot是什么框架

代码小兵99203-29 17:29

简述SpringBoot与Spring区别

杨晶珍08-02 11:09

使用Docker搭建开发环境

代码小兵98808-09 14:38

重启Docker容器命令

代码小兵87208-09 15:04

简述微服务的3个优点

代码小兵69606-10 10:02

发评论

举报

0/150

取消