SpringBoot+Mybatis简单的增删改查(注解实现)

工程创建选的是maven项目,目录结构如下

SpringBoot+Mybatis简单的增删改查(注解实现)_第1张图片

##pom.xml的依赖配置

 <!-- 指定maven仓库 -->
    <repositories><!-- 发现依赖和扩展的远程资源库 -->
        <!-- maven自己官方仓库 -->
        <repository>
            <id>maven</id>
            <name>Maven Repository Switchboard</name>
            <layout>default</layout>
            <url>http://repo1.maven.org/maven2</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <!-- 阿里巴巴发布版本仓库 -->
        <repository>
            <id>alibaba-opensource</id>
            <name>alibaba-opensource</name>
            <url>http://code.alibabatech.com/mvn/releases/</url>
            <layout>default</layout>
        </repository>
        <!-- 阿里巴巴快照版本仓库 -->
        <repository>
            <id>alibaba-opensource-snapshot</id>
            <name>alibaba-opensource-snapshot</name>
            <url>http://code.alibabatech.com/mvn/snapshots/</url>
            <layout>default</layout>
        </repository>
    </repositories>

    <!-- 继承父包 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <!-- spring-boot的web启动的jar包 -->
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 打成war包需要的配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>

        </dependency>
        <!-- 热启动-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- mysql数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>


        <!--thymeleaf模板jar,是很不错的html数据传递取值,类似jsp的jstl-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>

        </dependency>
        <!--jpa的jar包 ,操作数据库的,类似hibernate -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>


        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>
    </dependencies>
    </dependencies>

配置数据库数据源

在resource目录下新建application.properties文件里配置
SpringBoot+Mybatis简单的增删改查(注解实现)_第2张图片

#数据库数据源
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=666002
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

mybatis.mapper-locations=classpath:mapper/*Mapper.xml

配置mapper文件

resourcers目录下mapper目录
SpringBoot+Mybatis简单的增删改查(注解实现)_第3张图片

<?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.project.dao.IUserDao">
    <resultMap type="com.project.bean.UserBean" id="user">
        <id column="pk_id" property="userId" />
        <result column="f_name" property="userName"/>
        <result column="f_wd" property="pwd"></result>
    </resultMap>
</mapper>

用户实体类

package com.project.bean;

public class UserBean {

    private  int userId;

    private String userName;

    private  String pwd;

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return pwd;
    }

    public void setPassword(String password) {
        this.pwd = password;
    }
}

书写sql语句和IUserDao接口

 /**
     * 添加
     * @param user
     */
    @Insert("insert into t_user (f_name,f_pwd) values(#{userName},#{pwd})")
    public  void addUser(UserBean user);


    /**
     *
     * 查询所有
     * @return返回用户集合
     */
    @Select("select * from t_user")
    @ResultMap("user")
    public List<UserBean> findAll();

    /**
     * 通过用户名和密码实现用户登录
     * @param userName
     * @param pwd
     * @return
     */
    @Select("select * from t_user where f_name=#{userName} AND f_pwd=#{pwd}")
    @ResultMap("user")
    public UserBean login(@Param("userName") String userName, @Param("pwd") String pwd);

    /**
     * 通过id,修改用户密码
     * @param id
     * @param newPwd
     */
    @Update("update t_user set f_pwd=#{newPwd} where pk_id=#{userId}")
    public void updateUser(@Param("userId") int id,@Param("newPwd") String newPwd);


    /**
     * 通过id,删除用户
     * @param id
     */
    @Delete("delete from t_user where pk_id=#{userId}")
    public void deleteUser(@Param("userId") int id);

设计service接口和书写service实现类

Service接口

package com.project.service;

import com.project.bean.UserBean;
import org.apache.ibatis.annotations.Param;


import java.util.List;


public interface IUserService {

    public  void addUser(UserBean user);


    public UserBean login(String name,  String pwd);

    public List<UserBean> findAll();


    public void updateUser(int id,String newPwd);

    public void deleteUser(int id);
}

实现类

package com.project.service.impl;

import com.project.dao.IUserDao;
import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserDao dao;


    @Override
    public void addUser(UserBean user) {
        dao.addUser(user);
    }

    @Override
    public UserBean login(String name, String pwd) {
        return dao.login(name, pwd);
    }

    @Override
    public List<UserBean> findAll() {
        return dao.findAll();
    }

    @Override
    public void updateUser(int id, String newPwd) {
        dao.updateUser(id, newPwd);
    }

    @Override
    public void deleteUser(int id) {
        dao.deleteUser(id);
    }
}

配置SpringBootApplication

package com.project;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainBoot {
    public static void main(String[] args) {
        SpringApplication.run(MainBoot.class);
    }
}

书写测试类

package com.project.test;

import com.project.MyBaitsMain;
import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyBaitsMain.class)
public class MyTest {

    @Autowired
    private IUserService service;


    @Test
    public void loginTest(){
        UserBean user=service.login("Sam","777");
        System.out.println(user.getUserId());
    }

    @Test
    public void findAllTest(){
        List<UserBean> userList=service.findAll();
        for (UserBean user:userList){
            System.out.println(user.getUserName());
        }
    }

    @Test
    public void addUserTest(){
        UserBean user=new UserBean();
        user.setUserName("Chris");
        user.setPassword("111");
        service.addUser(user);
    }

    @Test
    public void updateTest(){
        service.updateUser(1,"555");
    }


    @Test
    public void deleteTest(){
        service.deleteUser(4);
    }
}

你可能感兴趣的:(SpringBoot+Mybatis简单的增删改查(注解实现))