码迷,mamicode.com
首页 > 编程语言 > 详细

springboot+mybatis整合,基本框架

时间:2020-07-14 16:35:31      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:resultmap   http   highlight   boot   package   xsd   java   base   build   

1.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.1.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.sdsd</groupId>
  <artifactId>mysql</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>mysql</name>
  <description>Demo project for Spring Boot</description>

  <properties>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.1.3</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-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.9</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

2.application.yml文件

spring:
  datasource:
    url: jdbc:mysql://ip:3306/数据库?useUnicode=true&characterEncoding=UTF8
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis: 
type-aliases-package: com.sdsd.mapper
mapper-locations: classpath:mapper/**/*.xml

type-aliases-package:mapper接口的路径

mapper-locations:映射的xml文件的路径

3.pojo实体类

public class PolicyStatus {
    private String pStateCode;
    private String pStateName;
    private String pStateType;
//setter,getter方法
}

4.mapper接口

import com.sdsd.pojo.PolicyStatus;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface PolicyMapper {

    List<PolicyStatus> selectAll();
}

5.mapper.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="com.sdsd.mapper.PolicyMapper">
    <resultMap id="BaseResultMap" type="com.sdsd.pojo.PolicyStatus" >
        <id column="p_state_code" property="pStateCode" jdbcType="VARCHAR" />
        <result column="p_state_name" property="pStateName" jdbcType="VARCHAR" />
        <result column="p_state_type" property="pStateType" jdbcType="VARCHAR" />
    </resultMap>

    <sql id="Base_Column_List" >
        p_state_code, p_state_name, p_state_type
    </sql>
    <select id="selectAll" resultMap="BaseResultMap" parameterType="com.sdsd.pojo.PolicyStatus">
        select <include refid="Base_Column_List"/>
        from t_cust_policy_state
    </select>
</mapper>

  注意:namespace的路径不要错了

6.调用mapper查询所有

@RestController
@RequestMapping("/test")
public class PolicyController {
    @Autowired
    PolicyMapper policyMapper;

    @RequestMapping("/select")
    public List<PolicyStatus> select(){
        return policyMapper.selectAll();
    }
}

  以上,基本的框架就搭建起来,类似数据库的连接数量,超时时间,连接池设置,事务管理等,可以根据需要写一个mybatisconfig.java,使用@configuration注解,以及@bean注解把需要的内容设置下;

springboot+mybatis整合,基本框架

标签:resultmap   http   highlight   boot   package   xsd   java   base   build   

原文地址:https://www.cnblogs.com/bin-zhao/p/13299644.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!