springboot+mysql 简单查询操作
一、项目的分层
1、controller层内容
package com.dream.controller; import com.dream.pojo.Users; import com.dream.service.user.UsersService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; @Controller public class PageController { //导入UsersService的包 @Autowired private UsersService usersService; @RequestMapping("/index") public String index(){ return "index"; } @RequestMapping("/login") public String login(){ return "/subpage/login"; } /* 登录验证 */ @RequestMapping("/unLogin") // @ResponseBody public String unLogin(HttpServletRequest request, Model model, HttpSession session){ String uname=request.getParameter("username"); // System.out.println("uname====>"+uname); String password = request.getParameter("password"); Users byId = usersService.getById(uname); //设置session的值是为了使用thymeleaf在前端的调用不写报错 session.setAttribute("all",byId); System.out.println(byId); if(byId==null){ model.addAttribute("msg","密码错误"); return "/subpage/login"; } return "redirect:/index"; } }
2、mapper层
package com.dream.mapper; import com.dream.pojo.Users; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; @Mapper @Repository public interface UsersMapper { Users getById(String StuNumber); }
3、pojo层
package com.dream.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.stereotype.Service; @Data @AllArgsConstructor @NoArgsConstructor public class Users { private int uId; private String uName; private String uPassword; private String uEmail; private String uTelephone; private int uFans; private int videoid; private String uhd; private int BlogId; private int Fabulous; private int EmId; private int DeleCommentNum; private int DeleblogNum; private int BlogNumber; private int videoNumber; private int Coin; private String BankCard; private String Classr; private String StuNumber; private String AdminId; private int Blacklistl; private String StateId; }
4、service层里的user包UsersService
package com.dream.service.user; import com.dream.pojo.Users; import org.springframework.stereotype.Service; @Service public interface UsersService { Users getById(String StuNumber); }
4.2、service层里的user包里的impl包UsersServiceImpl
package com.dream.service.user.impl; import com.dream.mapper.UsersMapper; import com.dream.pojo.Users; import com.dream.service.user.UsersService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UsersServiceImpl implements UsersService { @Autowired private UsersMapper usersMapper; @Override public Users getById(String StuNumber) { return usersMapper.getById(StuNumber); } }
5、resources里的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" > <mapper namespace="com.dream.mapper.UsersMapper"> <select id="getById" resultType="Users"> select * from users where StuNumber=#{StuNumber}; </select> </mapper>
6、application.YML
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/wuye_db?characterEncoding=utf8&useUnicode=true
username: root
password: root
server:
port: 8080
mvc:
static-path-pattern: /static/**
mybatis:
type-aliases-package: com.cc.test2.pojo
mapper-locations: classpath:mapper/*.xml
spring.thymeleaf.prefix: classpath:/templates/
spring.thymeleaf.suffix: .html
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/wuye_db?characterEncoding=utf8&useUnicode=true
username: root
password: root
server:
port: 8080
mvc:
static-path-pattern: /static/**
mybatis:
type-aliases-package: com.cc.test2.pojo
mapper-locations: classpath:mapper/*.xml
spring.thymeleaf.prefix: classpath:/templates/
spring.thymeleaf.suffix: .html
7.POM.XML
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.cc</groupId> <artifactId>Test2</artifactId> <version>0.0.1-SNAPSHOT</version> <name>Test2</name> <description>Test2</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.14</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.8.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <resources> <!-- <resource>--> <!-- <directory>src/main/java</directory>--> <!-- <includes>--> <!-- <include>**/*.xml</include>--> <!-- </includes>--> <!-- </resource>--> <resource> <directory>src/main/java</directory> <includes> <include>**/*.yml</include> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.yml</include> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>