• springboot+mybatis整合,基本框架


    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注解把需要的内容设置下;

  • 相关阅读:
    MySQL DDL 在指定位置新增字段
    .NET平台常见技术框架整理汇总
    使用Linq求和方法Sum计算集合中多个元素和时应该注意的性能问题
    python时间操作
    django 利用原生sql操作数据库
    滑动验证码获取像素偏移量
    python opencv简单使用
    django通过旧数据库库生成models
    pandas 操作csv文件
    简单工厂模式(Simple Factory Pattern)
  • 原文地址:https://www.cnblogs.com/bin-zhao/p/13299644.html
Copyright © 2020-2023  润新知