开发工具
1、Eclipse
2、Maven
3、Spring Boot
首先,Eclipse中配置好maven,具体请百度
工程结构:
实现步骤:
1、Eclipse中新建一个maven project
(1)src/main/java文件夹是默认的源代码文件,写业务逻辑代码
(2)src/main/resources是默认约定的静态资源目录
手动添加src/main/resources文件夹(注意文件是source folder类型),它里面是连接数据库的配置文件
2、就像第一步那样,新建一个maven project,这次基于webapp的,如下图,一直下一步
复制他的src/main/webapp文件夹和所有内容,拷贝到第一个工程下的src/main/下(非source folder文件类型)
3、pom.xml全部内容:
<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.zit</groupId> <artifactId>SpringBoot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.BUILD-SNAPSHOT</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-data-jpa</artifactId> </dependency> </dependencies><repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/libs-snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> </project>
可以看到,若要使用Spring Data JPA,直接dependency它即可,它会自动下载依赖jar包,非常方便
4、不要忘了,连接MySQL需要MySQL的连接jar包
mysql-connector-java-*-bin.jar
在工程下新建一个文件夹lib,存放该jar包,并add to build path
5、数据表
6、完整代码
(1)model类,对应数据库的表和字段
package com.zit.model; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import javax.validation.constraints.NotNull; @Entity @Table(name="logintable") public class UserLogin { @Id @NotNull @GeneratedValue(strategy = GenerationType.AUTO) private long id; @NotNull @Column(name="login_name") private String loginName; @NotNull @Column(name="login_password") private String loginPassword; @Column(name="address") private String address; public UserLogin(){} public UserLogin(String a, String b){ this.loginName = a; this.loginPassword = b; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } public String getLoginPassword() { return loginPassword; } public void setLoginPassword(String loginPassword) { this.loginPassword = loginPassword; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
(2)Dao层类
package com.zit.dao; import javax.transaction.Transactional; import org.springframework.data.repository.CrudRepository; import com.zit.model.UserLogin; @Transactional public interface UserLoginDao extends CrudRepository<UserLogin, Long>{ UserLogin findByloginName(String loginName); }
JPA的简便之处:简单的sql语句,直接通过约定的函数名称即可,不需要写sql语句
(3)Controller
package com.zit; import javax.annotation.Resource; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.zit.dao.UserLoginDao; import com.zit.model.UserLogin; @Controller @SpringBootApplication @EnableAutoConfiguration public class UserLoginController { @Resource UserLoginDao userLoginDAO; @RequestMapping("/userLogin") @ResponseBody public String login(String userName, String password){ UserLogin ul = userLoginDAO.findByloginName(userName); if(ul==null) { return "Login Error"; } else { return ul.getLoginName()+"----"+ul.getLoginPassword() + "----" +ul.getAddress(); } } public static void main(String[] args) { SpringApplication.run(UserLoginController.class, args); } }
(4)application.properties
#DB Configuration: spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.url = jdbc:mysql://localhost:3306/test?characterEncoding=utf-8 spring.datasource.username = root spring.datasource.password = #JPA Configuration: spring.jpa.database=MySQL spring.jpa.show-sql=true spring.jpa.generate-ddl=true spring.jpa.hibernate.ddl-auto=update #spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy #spring.jpa.database=org.hibernate.dialect.MySQL5InnoDBDialect #spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MYSQL5Dialect
最后,在Controller类上右键,run as ———Java Application
浏览器输入地址:http://localhost:8080/userLogin?userName=li&password=1234567
页面显示内容