IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

SpringBoot集成Mybatis

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

1.创建SpringBoot工程

根据
http://tech.souyunku.com/vitasyuan/p/8765329.html
说明创建SpringBoot项目。

2.添加相关依赖

在pom.xml文件中添加数据库连接和mybatis的相关依赖,完整的pom文件如下:

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- 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>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

3.创建测试数据表

创建测试数据库:springbootdemo,并添加以下数据表:

CREATE TABLE `dictionary` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键',
  `dict_key` varchar(50) NOT NULL DEFAULT '' COMMENT '字典key',
  `dict_value` varchar(50) NOT NULL DEFAULT '' COMMENT 'value',
  `parent_id` int(11) NOT NULL COMMENT '上级节点id',
  `description` varchar(100) NOT NULL DEFAULT '' COMMENT '描述信息',
  PRIMARY KEY (`id`),
  KEY `Index_dictKey_parentId` (`dict_key`,`parent_id`)
) ENGINE=InnoDB AUTO_INCREMENT=34 DEFAULT CHARSET=utf8mb4 COMMENT='数据字典表';

4.添加数据库相关配置

数据库配置使用多环境配置,具体多环境配置方法参考:http://tech.souyunku.com/vitasyuan/p/8782612.html

在application-dev.properties配置文件中添加数据库相关配置:

#数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdemo
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

在application.properties文件中添加使用dev环境配置文件的内容:

#配置使用的配置环境,值为application-{profile}.properties中的profile值
spring.profiles.active=rc
#mapper文件的路径
mybatis.mapper-locations=classpath:mapper/**/*.xml

5.添加mapper文件和接口

在resource文件夹下添加mapper/demo-server文件夹,并添加dictionary.xml配置文件,配置文件内容如下:

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.demo.business.dictionary.dao.DictionaryDao">

    <resultMap id="DictionaryResultMap" type="com.example.demo.business.dictionary.Dictionary">
        <result property="id" column="id"></result>
        <result property="dictKey" column="dict_key"></result>
        <result property="dictValue" column="dict_value"></result>
        <result property="parentId" column="parent_id"></result>
        <result property="description" column="description"></result>
    </resultMap>

    <select id="list" resultMap="DictionaryResultMap">
        SELECT * FROM `dictionary`
    </select>

    <select id="listChildrenByKey" resultMap="DictionaryResultMap">
        SELECT * FROM dictionary where parent_id= (select id from dictionary where dict_key= #{key})
    </select>

    <delete id="delete" parameterType="int">
        delete from dictionary where id = #{id}
    </delete>

    <insert id="insert" parameterType="com.example.demo.business.dictionary.Dictionary">
        INSERT INTO `dictionary`(`dict_key`,`dict_value`,`parent_id`,`description`)
        VALUES(#{dictKey}, #{dictValue}, #{parentId}, #{description})
    </insert>
</mapper>

6.添加controller测试数据库连接

创建controller类,代码如下:

@RestController
@RequestMapping(value = "/dictionary")
public class DictionaryController {

    @Autowired
    private DictionaryDao dictionaryDao;

    @GetMapping
    public Response<List<Dictionary>> get(){
        Response<List<Dictionary>> response = new Response<>();
        response.setData(dictionaryDao.list());
        return  response;
    }
}

启动服务,在浏览器中输入访问url:

http://localhost:8080/demo/dictionary

返回以下数据:

{
  "code": 200,
  "message": "Success",
  "data": [
    {
      "id": 27,
      "dictKey": "test",
      "dictValue": "testvalue",
      "parentId": 1,
      "description": "test"
    },
    {
      "id": 30,
      "dictKey": "test",
      "dictValue": "testvalue",
      "parentId": 1,
      "description": "test"
    },
    {
      "id": 32,
      "dictKey": "test",
      "dictValue": "testvalue",
      "parentId": 1,
      "description": "test"
    },
    {
      "id": 33,
      "dictKey": "test",
      "dictValue": "testvalue",
      "parentId": 1,
      "description": "test"
    }
  ]
}

表示数据库配置成功。

文章永久链接:https://tech.souyunku.com/?p=32201


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(73) 打赏



未经允许不得转载:搜云库技术团队 » SpringBoot集成Mybatis

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367