SpringBoot整合mybatis

版权声明:转载请附上链接!谢谢 https://blog.csdn.net/qq_26173219/article/details/80841863

首先创建一个springboot项目,如果不会可以先百度,或者https://start.spring.io/ 可以直接生成一个springboot项目,我就是这样弄的。
下载好以后导入到eclipse
项目结构
这里写图片描述
先安装好mybatis-generator插件,不会的可以看这篇https://blog.csdn.net/qq_26173219/article/details/80842067

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.shz</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</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>1.3.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>
    </dependencies>

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


</project>

这里不使用application.properties文件 而使用更加简洁的application.yaml文件,可以直接重命名把后缀改为yaml

server: 
  port: 8080

spring: 
    datasource: 
        url: jdbc:mysql://换成你的链接:3306/数据库名称
        username: root
        password: 123456
        driver-class-name: com.mysql.jdbc.Driver
mybatis: 
  typeAliasesPackage: com.shz.model
  mapperLocations: classpath:mapper/*.xml

创建好相应的包
这里写图片描述

然后看一下mybatis-generator.xml,把相关的路径替换成你自己的

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>

    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接地址账号密码-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://数据库连接:3306/数据库名称" userId="root" password="123456">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成Model类存放位置-->
        <javaModelGenerator targetPackage="com.shz.demo.model" targetProject="demo/src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
         <!--对应的mapper.xml文件  -->  
        <sqlMapGenerator targetPackage="mapper" targetProject="demo/src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!--生成Dao类存放位置-->
        <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
                type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
                type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
                type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
        -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.shz.demo.dao" targetProject="demo/src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--生成对应表及类名-->
        <table tableName="t_users" domainObjectName="Users" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <!--<table tableName="t_goods" domainObjectName="Goods" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
           -->     <!--enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"-->
               <!--selectByExampleQueryId="false"/>-->

    </context>
</generatorConfiguration>

替换好以后点击运行这里写图片描述
生成成功!
这里写图片描述

然后我们把相关的action和service等写一下
UsersAction

package com.shz.demo.action;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shz.demo.model.Users;
import com.shz.demo.service.UserService;

@Controller
@RequestMapping(value = "/users")
public class UsersAction {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/saveUsers")
    @ResponseBody
    public int saveUsers(String name, String pwd) {
        Users users = new Users();
        users.setName(name);
        users.setPwd(pwd);
        userService.saveUsers(users);
        return 1;
    }

}

UserService

package com.shz.demo.service;

import com.shz.demo.model.Users;

public interface UserService {

    void saveUsers(Users users);

}

UserServiceImpl

package com.shz.demo.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shz.demo.dao.UsersMapper;
import com.shz.demo.model.Users;
import com.shz.demo.service.UserService;
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public void saveUsers(Users users) {
        usersMapper.insert(users);
    }
}
package com.shz.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.shz.demo.dao")//这里一定不要忘记
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

右键启动项目

数据库显示是空的
这里写图片描述
我们发送一个请求
这里写图片描述

debug到后台看看值传过去没有
这里写图片描述
没毛病,返回1
这里写图片描述

这里写图片描述

其他的操作就不写了,都一样

项目资源:https://download.csdn.net/download/qq_26173219/10505298

展开阅读全文

没有更多推荐了,返回首页