工程结构

 

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">
    <parent>
        <artifactId>springboot-demo</artifactId>
        <groupId>cn.xiaojf</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <artifactId>springboot-mybatis-plus</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>2.1.7</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatisplus-spring-boot-starter</artifactId>
            <version>1.0.5</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.45</version>
        </dependency>
    </dependencies>
</project>

application.properties

#应用端口号
server.port=8010
#freemarker 默认文件后缀
spring.freemarker.suffix=.html

#数据库设置
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root

#mybatis plus 设置
mybatis-plus.mapper-locations=classpath:/mapper/*Mapper.xml
#实体扫描,多个package用逗号或者分号分隔
mybatis-plus.typeAliasesPackage=cn.xiaojf.springboot.mybatisplus.entity
#主键类型  0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
mybatis-plus.global-config.id-type=2
#字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
mybatis-plus.global-config.field-strategy=2
#驼峰下划线转换
mybatis-plus.global-config.db-column-underline=true
#刷新mapper 调试神器
mybatis-plus.global-config.refresh-mapper=true
#数据库大写下划线转换
#mybatis-plus.global-config.capital-mode=true
#序列接口实现类配置
#mybatis-plus.global-config.key-generator=com.baomidou.springboot.xxx
#逻辑删除配置
mybatis-plus.global-config.logic-delete-value=0
mybatis-plus.global-config.logic-not-delete-value=1
#自定义填充策略接口实现
#mybatis-plus.global-config.meta-object-handler=com.baomidou.springboot.xxx
#自定义SQL注入器
#mybatis-plus.global-config.sql-injector=com.baomidou.springboot.xxx
mybatis-plus.configuration.map-underscore-to-camel-case=true
mybatis-plus.configuration.cache-enabled=false

 MybatisPlusConfig.java

package cn.xiaojf.springboot.mybatisplus.configure;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan("cn.xiaojf.springboot.mybatisplus.mapper*")
public class MybatisPlusConfig {

}

UserMapper.java

package cn.xiaojf.springboot.mybatisplus.mapper;

import cn.xiaojf.springboot.mybatisplus.SuperMapper;
import cn.xiaojf.springboot.mybatisplus.entity.User;

import java.util.List;

public interface UserMapper extends SuperMapper<User> {
    List<User> findByUserName(String name);

    User findUserAddrByName(String name);
}

UserMapper.xml

<?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="cn.xiaojf.springboot.mybatisplus.mapper.UserMapper">
    <resultMap id="userAddr" type="cn.xiaojf.springboot.mybatisplus.entity.User">
        <id column="user_id" property="id"></id>
        <result column="user_name" property="name"></result>
        <result column="user_age" property="age"></result>
        <collection property="userAddrList" ofType="cn.xiaojf.springboot.mybatisplus.entity.UserAddr">
            <id column="addr_id" property="id"></id>
            <result column="addr_name" property="name"></result>
            <result column="user_id" property="userId"></result>
        </collection>
    </resultMap>

    <select id="findByUserName" resultType="cn.xiaojf.springboot.mybatisplus.entity.User">
      SELECT * FROM sys_user
    </select>

    <select id="findUserAddrByName" resultMap="userAddr">
        SELECT
            u.id AS user_id,u.`name` AS user_name ,u.age AS user_age,addr.`name` AS addr_name,addr.id AS addr_id
        FROM
            sys_user u
        LEFT JOIN sys_user_addr addr ON u.id = addr.user_id
        WHERE
          u.name = #{name}
    </select>

</mapper>

 

源码

https://gitee.com/xiaojf/springboot-demo/tree/master/springboot-mybatis-plus

 

posted on 2017-12-25 16:31  肖建锋  阅读(9604)  评论(0编辑  收藏  举报