springboot和mybatis项目学习

#项目整体样貌
在这里插入图片描述
##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层
    @Autowired
    UserService 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层

    @Autowired
    private 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);
    }


    @Autowired
    private 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.Driver
    url: jdbc:mysql://localhost:3306/firstdatabase?serverTimezone=GMT%2B8&useSSL=false&useUnicode=true&characterEncoding=utf-8
    username: root
    password:
mybatis:
  type-aliases-package: com.example.demo.bean
  mapper-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>

#效果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yyd__

你的打赏和鼓励我会珍惜!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值