#项目整体样貌
##bean
package com.example.demo.bean;public class informationBean {private int id;private String name;private String password;private String attchfile;public int getId() {return id;}public String getName() {return name;}public String getPassword() {return password;}public String getAttchfile() {return attchfile;}
}
package com.example.demo.bean;public class UserInfo {private int id;private String name;private String password;private String attchfile;public int getId() {return id;}public String getName(){return name;}public String getPassword(){return password;}}
##controller
package com.example.demo.controller;import com.example.demo.bean.UserInfo;
import com.example.demo.bean.informationBean;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/test")
public class LoginController {//将Service注入Web层@AutowiredUserService userService;@ResponseBody@RequestMapping("/login")public String login(){return "login";}@ResponseBody@RequestMapping(value = "/loginIn")public informationBean loginIn(@RequestParam String name, @RequestParam String password){//UserBean userBean = userService.loginIn(name,password);informationBean userBean = userService.getInfore(name,password);if(userBean!=null){return userBean;}else {return null;}}@ResponseBody@RequestMapping("/getid")public UserInfo userId(Integer id) { // 使用实体类对象和接口方法if (id == null) {System.out.println("id is null");return null;}return userService.userId(id);}
}
##mapper
package com.example.demo.mapper;import com.example.demo.bean.informationBean;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;@Mapper
@Repository
public interface nameMapper {informationBean getInfo(@Param("name")String name, @Param("password")String password);
}
package com.example.demo.mapper;import com.example.demo.bean.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;@Mapper
@Repository
public interface UserMapper {UserInfo userId(@Param("id")Integer id);
}
##service接口和实现
package com.example.demo.service;import com.example.demo.bean.UserInfo;
import com.example.demo.bean.informationBean;public interface UserService {informationBean getInfore(String name, String password);UserInfo userId(Integer id);
}
package com.example.demo.ServiceImpl;import com.example.demo.bean.UserInfo;
import com.example.demo.bean.informationBean;
import com.example.demo.mapper.UserMapper;
import com.example.demo.mapper.nameMapper;
import com.example.demo.service.UserService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;@MapperScan
@Service
public class UserServiceImpl implements UserService {//将DAO注入Service层@Autowiredprivate nameMapper a;public informationBean getInfore(String name, String password){System.out.println("userMapper.userId(id)");informationBean res = a.getInfo(name,password);if(res == null){System.out.println("userMapper.userId(id) is null");return null;}System.out.println("userMapper.userId(id)"+res.getId());return a.getInfo(name,password);}@Autowiredprivate UserMapper userMapper;public UserInfo userId(Integer id){ // 此处UserInfo为实体类对象,userId 为数据持久层接口方法System.out.println("public UserInfo userId(Integer id)");UserInfo RES = userMapper.userId(id);if(RES == null){return null;}//System.out.println("userMapper.userId(id)"+RES.getId());return RES; // 返回接口方法中的id,这里报错}}
#启动类
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.mapper")
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}
##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.example.demo.mapper.nameMapper"><!--com.example.demo.bean.informationBean-->
<select id="getInfo" parameterType="String" resultType="informationBean">SELECT id,name,password,attchfile FROM password WHERE name = #{name} AND password = #{password}
</select></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.example.demo.mapper.UserMapper"><!--com.example.demo.bean.informationBean--><select id="userId" parameterType="Integer" resultType="UserInfo">SELECT id,name,password,attchfile FROM password WHERE id = #{id}</select></mapper>
##application.yml
server:port: 8080
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/firstdatabase?serverTimezone=GMT%2B8&useSSL=false&useUnicode=true&characterEncoding=utf-8username: rootpassword:
mybatis:type-aliases-package: com.example.demo.beanmapper-locations: classpath:mapper/*.xml
#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.7.1</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>demo</description><properties><java.version>8</java.version><maven-jar-plugin.version>3.1.1</maven-jar-plugin.version></properties><dependencies><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.3.1</version></dependency><!--启动依赖3.0.3--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.3.22</version></dependency><!-- <dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency>--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!--<dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><version>8.0.31</version><scope>runtime</scope></dependency>--></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><version>2.7.1</version><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
#效果