Mybatis代码生成及springboot基础环境搭建

一、springboot基础搭建

1. 使用IDEA构建springboot web框架

具体查阅网上相关博客

2. 使用Mybatis Generator生成Entity,Dao,Mapper

  1. 官方文档如下 Mybatis Generator
  2. 在maven中加入Mybatis Generator生成器插件
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
  1. 编码配置文件,配置文件的默认名为generatorConfig.xml,将该文件放于resources下, 以下是我的一个Demo的配置。
<?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>
    <classPathEntry location="E:\tool\apache-maven-3.5.0-bin\repo\mysql\mysql-connector-java\5.1.35\mysql-connector-java-5.1.35.jar" />

    <context id="mysqlTables" targetRuntime="MyBatis3">
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/sbshiro"
                        userId="root"
                        password="root">
        </jdbcConnection>

        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <javaModelGenerator targetPackage="com.howen.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="mappers"  targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER" targetPackage="com.howen.dao"  targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <table tableName="user" domainObjectName="User" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
                       enableUpdateByExample="false"/>

    </context>
</generatorConfiguration>

执行maven install以后将在com.howen.model下生成实体类,在com.howen.dao下生成Dao文件,在resources/mappers下生成映射文件。更多细节查阅官方文档

3. 连接MySql数据库

  1. Maven中添加MySqlMybatis依赖,当前版本为
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
        <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>
            <version>5.1.35</version>
        </dependency>
  1. application.properties中配置相关数据的连接,以及映射文件所在的位置
# 驱动配置信息
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/sbshiro
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver

# mybatis
mybatis.mapper-locations=classpath:mappers/*.xml
  1. 各层之间代码,以及配置
    • 实体类
package com.howen.model;

public class User {

    private Integer uid;

    private String username;

    private String password;

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getPassword() {
        return password;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}
  • dao层,可以采用注解@Mapper标记也可以采用@MapperScan进行包的扫描
package com.howen.dao;

import com.howen.model.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper {

    int deleteByPrimaryKey(Integer uid);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer uid);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}
  • service层,接口
package com.howen.service;

import com.howen.model.User;

public interface UserService {
    public User selectByPrimaryKey(Integer uid);
}
  • service层,实现类
package com.howen.service.serviceImpl;

import com.howen.dao.UserMapper;
import com.howen.model.User;
import com.howen.service.UserService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User selectByPrimaryKey(Integer uid) {
        return userMapper.selectByPrimaryKey(uid);
    }
}
  • controller层,RestController返回的是json数据,不指定页面
package com.howen.controller;

import com.howen.model.User;
import com.howen.service.serviceImpl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("/getUser")
    public String getUser(Integer uid){
        User user = userService.selectByPrimaryKey(uid);
        return user.toString();
    }
}

这里是将启动类放到所有各包的上一级目录中

springboot热部署

在pom.xml中加入

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <version>2.0.3.RELEASE</version>
</dependency>

猜你喜欢

转载自blog.csdn.net/lianowen/article/details/81202023