第四篇 - Spring Boot写一个登录功能1
SpringBoot + Mysql + jdk8
一、新建Spring Boot项目
创建成功后的目录为
创建多个package,结构如下:
先尝试build一下项目
报错java: 警告: 源发行版 17 需要目标发行版 17
Rebuild一下项目
报错类文件具有错误的版本 61.0, 应为 52.0
重启IDEA,Rebuild项目,成功。
接下来尝试Run一下项目
报错Unable to instantiate org.mybatis.spring.boot.autoconfigure.MybatisDependsOnDatabaseInitializationDetector
二、配置MySQL环境
参考链接:https://zhuanlan.zhihu.com/p/348166709
pom.xml添加依赖
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency>
application.properties添加配置
spring.datasource.url=jdbc:mysql://localhost:3307/test?useUnicode=true&characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.properties.hibernate.hbm2ddl.auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.show-sql= true
在controller文件夹下新建一个UserController类
package com.example.demo.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @RequestMapping("/getuserlist") public String getUserList() { return "获取成功!"; } }
新建一个entity文件夹并再此文件夹下新建一个User类
package com.example.demo.entity; public class User { public String user_account; public String user_password; public String getUsername() { return user_account; } public void setUsername(String User_account) { this.user_account = User_account; } public String getUserpassword() { return user_password; } public void setUserpassword(String User_password) { this.user_password = User_password; } }
在mapper文件夹下新建一个UserMapper类
package com.example.demo.mapper; import com.example.demo.entity.User; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Component; import java.util.List; @Component public interface UserMapper { //然后做一些数据库的对应操作,这次是查询操作 @Select("select * from user") List<User> getUserList(); }
文件目录
UserService.java
package com.example.demo.service; import com.example.demo.entity.User; import org.springframework.stereotype.Service; import java.util.List; @Service public interface UserService { //和mapper相呼应,有一个对应的方法 List<User> getUserList(); }
UserServiceImpl.java
package com.example.demo.service.impl; import com.example.demo.entity.User; import com.example.demo.mapper.UserMapper; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import java.util.List; @Repository public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public List<User> getUserList() { try{ List<User> users= userMapper.getUserList(); return users; }catch (Exception e){ //好像是 如果不throw的话,会报错。。。 e.printStackTrace(); throw e; } } }
修改UserController.java
package com.example.demo.controller; import com.example.demo.entity.User; import com.example.demo.service.impl.UserServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class UserController { @Autowired private UserServiceImpl userServer; @RequestMapping("/getuserlist") public List<User> getUserList() { return userServer.getUserList(); } }
最后在Demo1Application.java中添加注解
package com.example.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.example.demo") public class Demo1Application { public static void main(String[] args) { SpringApplication.run(Demo1Application.class, args); } }
报错类文件具有错误的版本 61.0, 应为 52.0
重启IDEA,运行项目,成功。
访问http://localhost:8080/getuserlist