MyBatis 配置与使用


第一次接触java,记录下点滴

pom

<?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.skyhyko</groupId>
    <artifactId>FirstProj</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>FirstProj</name>
    <description>FirstProj</description>
    <properties>
        <java.version>17</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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <type>jar</type>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

配置yml

删除resource中的application.properties,添加application.yml与application-dev.yml

application.yml

spring:
  profiles:
    active: dev

application-dev.yml

server:
  port: 8080
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false  #3306/后面是数据库的名字
    username: root
    password: root
mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml

编写Dao,Model,Service,Controller

UserInfoMapper

package com.skyhyko.dao;

import com.skyhyko.model.UserInfo;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserInfoMapper {

    public List<UserInfo> findAllUser();

    int deleteByPrimaryKey(Integer id);

    UserInfo selectByPrimaryKey(Integer id);

    UserInfo findByID(Integer id);

    int insertUser(UserInfo user);
}

UserInfo

package com.skyhyko.model;

public class UserInfo {

    private Integer id;
    private String username;
    private String password;
    private Integer age;
    private String tel;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getid() {
        return id;
    }

    public void setid(Integer id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel == null ? null : tel.trim();
    }

    @Override
    public String toString() {
        return "User{" +
                "userid=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age='" + age + '\'' +
                ", tel='" + tel + '\'' +
                '}';
    }
}

UserService

package com.skyhyko.service;

import com.skyhyko.dao.UserInfoMapper;
import com.skyhyko.model.UserInfo;
import java.util.List;

import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired(required=false)
    public UserInfoMapper userMapper;

    public List<UserInfo> findAllUser(){
        return userMapper.findAllUser();
    }

    public UserInfo findByID(Integer id){
        UserInfo user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    public int insert(UserInfo user){
        return userMapper.insertUser(user);
    }

}

UserController

package com.skyhyko.controller;

import com.skyhyko.service.UserService;
import com.skyhyko.model.UserInfo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/getAllUser")
    public List<UserInfo> findAll(){
        return userService.findAllUser();
    }

    @RequestMapping("/getUser/{id}")
    public UserInfo findUserByid(@PathVariable Integer id){
        return userService.findByID(id);
    }

    @RequestMapping("/insertUser")
    public int insertUser(UserInfo user){
        return userService.insert(user);
    }
}

编写mapper.xml

一定要确保对应关系正确,否则报错Invalid bound statement (not found)
在这里插入图片描述

<?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.skyhyko.dao.UserInfoMapper" ><!-- 对应你的dao文件 -->
  <resultMap id="result" type="com.skyhyko.model.UserInfo" >
      <result column="id" jdbcType="INTEGER" property="id"/>
      <result column="UserName" jdbcType="VARCHAR" property="UserName"/>
      <result column="Password" jdbcType="VARCHAR" property="Password"/>
      <result column="Age" jdbcType="INTEGER" property="Age"/>
      <result column="Tel" jdbcType="VARCHAR" property="Tel"/>
  </resultMap>
  <select id="findAllUser" resultType="com.skyhyko.model.UserInfo">
    select  * from userinfo;
  </select>
  <select id="findByID" resultType="com.skyhyko.model.UserInfo">
    select  * from userinfo where id = #{id}
  </select>
    <insert id="insertUser"  parameterType="com.skyhyko.model.UserInfo">
    insert into userinfo(UserName,Password,Age,Tel) values (#{username},#{password},#{age},#{tel})
    </insert>
  <select id="selectByPrimaryKey" resultType="com.skyhyko.model.UserInfo">
    select *  from userinfo  where id = #{id}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from userinfo  where id = #{id}
  </delete>
</mapper>

测试

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值