欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

连接数据库

程序员文章站 2022-04-04 23:24:07
...

连接数据库

添加依赖
连接数据库

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.cww</groupId>
  <artifactId>wwayt</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>springboot</name>
  <description>springbootwwayt</description>

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>3.8.1</version>
              <scope>test</scope>
        </dependency>
        <!-- servlet依赖. -->
        <dependency>
              <groupId>javax.servlet</groupId>
              <artifactId>javax.servlet-api</artifactId>
        </dependency>
              <dependency>
                     <groupId>javax.servlet</groupId>
                     <artifactId>jstl</artifactId>
              </dependency>
        <!-- tomcat的支持.-->
        <dependency>
               <groupId>org.apache.tomcat.embed</groupId>
               <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <!-- 这个需要为 true 热部署才有效 -->
        <dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-devtools</artifactId>
		    <optional>true</optional> 
		</dependency> 
		 <!-- mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>
        <!-- jpa-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency> 
    </dependencies>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

maven更新项目
创建application.properties文件。编写连接数据库配置

#数据库配置、自动更新表结构
spring.datasource.url=jdbc:mysql://localhost:3306/my_website?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#JPA
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.show-sql= true

编写用户实体类

package com.cww.user.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "sys_user")
public class User {
	
	/*
	 * @GeneratedValue注解存在的意义主要就是为一个实体生成一个唯一标识的主键、@GeneratedValue提供了主键的生成策略。
	 * @GeneratedValue注解有两个属性,分别是strategy和generator,
	 * strategy属性:提供四种值:
	 *	-AUTO主键由程序控制, 是默认选项 ,不设置就是这个
	 *	-IDENTITY 主键由数据库生成, 采用数据库自增长, Oracle不支持这种方式
	 *	-SEQUENCE 通过数据库的序列产生主键, MYSQL  不支持
	 *  -Table 提供特定的数据库产生主键, 该方式更有利于数据库的移
	 */
	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private long id;
	
	@Column(name = "username")
	private String username;
	
	@Column(name = "password")
	private String password;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}

UserDao

package com.cww.user.dao;

import org.springframework.data.jpa.repository.JpaRepository;

import com.cww.user.entity.User;

public interface UserDao extends JpaRepository<User,Long>{

	User getUserByUsernameAndPassword(String username,String password);
}

UserService

package com.cww.user.service;

import org.springframework.stereotype.Service;
import com.cww.user.entity.User;

@Service
public interface UserService {
	
	public User login(User user);
	
}

UserServiceImpl

package com.cww.user.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cww.user.dao.UserDao;
import com.cww.user.entity.User;

@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
	UserDao userDao;
	
	@Override
	public User login(User user) {
		return userDao.getUserByUsernameAndPassword(user.getUsername(), user.getPassword());
	}

}

UserConrtoller

package com.cww.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cww.user.entity.User;
import com.cww.user.service.UserService;

@Controller
public class UserController {
	
	@Autowired
	UserService userService;
	
	/*
	 * 用户登录
	 */
	@RequestMapping("/login")
	@ResponseBody
	public String login(User user){
		User u = userService.login(user);
		if(u!=null)
			return "登录成功";
		else
			return "登录失败";
	}
}

准备数据
连接数据库
启动项目进行测试
连接数据库
连接数据库
项目目录
连接数据库
下一篇:thymleaf