zoukankan      html  css  js  c++  java
  • springboot和mybatis集成

    springboot和mybatis集成

    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>
        <groupId>com.liwei</groupId>
        <artifactId>leshop</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>leshop</name>
        <description>Demo project for Spring Boot</description>
     
        <properties>
            <java.version>1.8</java.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
        </properties>
     
        <dependencies>
            <!-- spring boot的核心starter -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!-- spring boot提供的辅助开发工具 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.0</version>
            </dependency>
            <!-- MySQL数据库连接需要的依赖 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.6</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
     
            <!-- json处理相关的依赖 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.45</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <!-- 系统监控相关的依赖 -->
            <!-- 系统监控相关的依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <!-- cglib的BeanCopier需要的依赖 -->
            <dependency>
                <groupId>asm</groupId>
                <artifactId>asm</artifactId>
                <version>3.3.1</version>
            </dependency>
            <dependency>
                <groupId>asm</groupId>
                <artifactId>asm-commons</artifactId>
                <version>3.3.1</version>
            </dependency>
            <dependency>
                <groupId>asm</groupId>
                <artifactId>asm-util</artifactId>
                <version>3.3.1</version>
            </dependency>
            <dependency>
                <groupId>cglib</groupId>
                <artifactId>cglib-nodep</artifactId>
                <version>2.2.2</version>
            </dependency>
            <!-- 测试需要的依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
        </dependencies>
     
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>${spring-boot.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
     
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>2.3.7.RELEASE</version>
                    <configuration>
                        <mainClass>com.liwei.leshop.LeshopApplication</mainClass>
                    </configuration>
                    <executions>
                        <execution>
                            <id>repackage</id>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
     
     </project>
     

    propertys

     # 应用名称
     spring.application.name=leshop
     server.port=8080
     spring.datasource.username=iexe
     spring.datasource.password=Shgbit123!@#
     spring.datasource.url=jdbc:mysql://47.103.165.132:3306/eshop?useUnicode=true&characterEncoding=utf-8  
     #spring.datasource.driver-class-name=com.mysql.jdbc.Driver
     mybatis.mapper-locations=classpath:mapper/*Mapper.xml
     mybatis.type-aliases-package=com.liwei.leshop.domain
     logging.level.com.example.mapper=debug
     spring.datasource.driverClassName=com.mysql.jdbc.Driver
     # 连接池的配置信息
     spring.datasource.initialSize=50  
     spring.datasource.minIdle=50  
     spring.datasource.maxActive=500  
     spring.datasource.maxWait=60000  
     spring.datasource.timeBetweenEvictionRunsMillis=60000  
     spring.datasource.minEvictableIdleTimeMillis=300000  
     spring.datasource.validationQuery=SELECT 1 FROM DUAL  
     spring.datasource.testWhileIdle=true  
     spring.datasource.testOnBorrow=false  
     spring.datasource.testOnReturn=false  
     spring.datasource.poolPreparedStatements=true  
     spring.datasource.maxPoolPreparedStatementPerConnectionSize=20  
     spring.datasource.filters=stat,wall,log4j  
     spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

    启动类

     /**
      * 集成springboot,mybatis
      */
     @MapperScan("com.liwei.leshop.dao")//使用MapperScan批量扫描所有的Mapper接口;
     @SpringBootApplication
     public class LeshopApplication {
     
        public static void main(String[] args) {
            SpringApplication.run(LeshopApplication.class, args);
        }
     
     }
     

    dao

     @Repository
     public interface UserMapper {
        List<User> findAll();
     }
     

    实体类

     @Data
     public class User {
        private String id;//编号
        private String username;//用户名
        private String password;//密码
     }
     

    resource/mapper/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="com.liwei.leshop.dao.UserMapper">
        <resultMap id="BaseResultMap" type="com.liwei.leshop.domain.User">
            <result column="id" jdbcType="VARCHAR" property="id" />
            <result column="userName" jdbcType="VARCHAR" property="username" />
            <result column="passWord" jdbcType="VARCHAR" property="password" />
        </resultMap>
        <select id="findAll" resultType="com.liwei.leshop.domain.User">
            select * from user
        </select>
     
     </mapper>

    数据库

     
     CREATE TABLE `user` (
      `id` varchar(32) NOT NULL AUTO_INCREMENT,
      `userName` varchar(32) NOT NULL,
      `passWord` varchar(50) NOT NULL,
      `realName` varchar(32) DEFAULT NULL,
      PRIMARY KEY (`id`)
     ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
     

    test

     package com.liwei.leshop.dao;
     
     import com.liwei.leshop.LeshopApplication;
     import com.liwei.leshop.domain.User;
     import org.junit.Test;
     import org.junit.runner.RunWith;
     import org.springframework.beans.factory.annotation.Autowired;
     import org.springframework.boot.test.context.SpringBootTest;
     import org.springframework.test.context.junit4.SpringRunner;
     
     import java.util.List;
     
     @RunWith(SpringRunner.class)
     @SpringBootTest(classes = LeshopApplication.class)
     
     public class UserMapperTest {
     @Autowired
         private UserMapper userMapper;
         @Test
         public void findAll() {
             List<User> all = userMapper.findAll();
             System.out.println(all);
        }
     }

     

  • 相关阅读:
    通用数据权限的思考与设计
    MyBatis传入参数为list、数组、map写法
    MyBatis的foreach查询(List、Array、Map)
    heX——基于 HTML5 和 Node.JS 开发桌面应用
    优秀设计:12个带给你灵感的创意单页网站作品
    So Easy!让开发人员更轻松的工具和资源
    触摸手势图标大全:48款触摸手势图标免费下载
    放松的周末,一起欣赏15个华丽的艺术品
    Skytte:一款令人印象深刻的 HTML5 射击游戏
    分享本年度最佳的15个 Photoshop 实例教程
  • 原文地址:https://www.cnblogs.com/q1359720840/p/14790012.html
Copyright © 2011-2022 走看看