个人项目实战1,搭建springboot+mybatis框架项目
Posted weixin_46401390
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了个人项目实战1,搭建springboot+mybatis框架项目相关的知识,希望对你有一定的参考价值。
文章目录
前言
由于之前有使用springMVC做了一个个人的项目,但是想重构成springboot+mybatis框架的项目,因此借此机会顺便记录下搭建一套简单的springboot+mybatis+mysql+阿里数据源的项目的步骤
一,效果图
搭建的是springboot+mybatis+阿里数据源+mysql的框架
二,步骤
1,idea创建项目
选择组件
然后一直下一步就ok了!
或者通过手动添加pom配置是一样的效果,我的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.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zhangb</groupId>
<artifactId>family</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>family</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>
</properties>
<dependencies>
<!--springboot组件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot整合mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.22</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2,编写application.yml配置
(1)设置端口
(2)设置数据库配置
(3)设置mybatis扫描路径
server:
port: 8086
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/doctorreimbursement?useUnicode=true&characterEncoding=utf8
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource # 使用druid数据源
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
mybatis:
mapper-locations: classpath:mapper/**/*.xml #注意:一定要对应mapper映射xml文件的所在路径
type-aliases-package: com.zhangb.**.entity # 注意:对应实体类的路径
3,编写服务启动入口
(1)设置组件扫描路径
(2)设置数据源为阿里数据源
package com.zhangb;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import javax.sql.DataSource;
@SpringBootApplication
@ComponentScan("com.zhangb")
public class FamilyApplication
public static void main(String[] args)
SpringApplication.run(FamilyApplication.class, args);
@Bean(destroyMethod = "close", initMethod = "init")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource()
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
4,编写业务测试接口
(1)controller
package com.zhangb.family.doctor.controller;
import com.zhangb.family.common.module.ViewData;
import com.zhangb.family.doctor.service.ReimbYlCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by z9104 on 2021/5/5.
*/
@RestController
@RequestMapping("/ylCard")
public class ReimbYlCardController
@Autowired
private ReimbYlCardService reimbYlCardService;
/**
* http://localhost:8086/ylCard/getYlCardCount
* @return
*/
@RequestMapping("/getYlCardCount")
public ViewData getYlCardCount()
ViewData viewData = new ViewData();
viewData.setObj(reimbYlCardService.getYlCardCount());
return viewData;
(2)Service
package com.zhangb.family.doctor.service;
/**
* Created by z9104 on 2021/5/5.
*/
public interface ReimbYlCardService
Integer getYlCardCount();
package com.zhangb.family.doctor.service.impl;
import com.zhangb.family.doctor.dao.ReimbYlCardDao;
import com.zhangb.family.doctor.service.ReimbYlCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 医疗账户业务类
*/
@Service
public class ReimbYlCardServiceImpl implements ReimbYlCardService
@Autowired
private ReimbYlCardDao reimbYlCardDao;
@Override
public Integer getYlCardCount()
return reimbYlCardDao.getYlCardCount();
(3)Dao
package com.zhangb.family.doctor.dao;
import org.apache.ibatis.annotations.Mapper;
/**
* Created by z9104 on 2021/5/5.
*/
@Mapper
public interface ReimbYlCardDao
/**
* 查询医疗账户数量
* @return
*/
Integer getYlCardCount();
<?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.zhangb.family.doctor.dao.ReimbYlCardDao">
<resultMap id="baseMap" type="com.zhangb.family.doctor.entity.ReimbYlCard">
<result column="oid" property="oid"/>
<result column="yl_card" jdbcType="VARCHAR" property="ylCard"/>
<result column="master_name" jdbcType="VARCHAR" property="masterName"/>
<result column="created_date" property="createdDate"/>
</resultMap>
<!--查询医疗账号数量-->
<!--select oid,yl_card,master_name,created_date from tool_yl_card_t-->
<select id="getYlCardCount" resultType="integer" >
select count(1) from tool_yl_card_t
</select>
</mapper>
5,启动并测试
http://localhost:8086/ylCard/getYlCardCount
其中数据库表:
总结
重构第一步搭建框架已完成,总体来说很简单没遇到什么坑。
下一篇:【个人项目实战】2,springboot集成(Html+vue.js)前端框架
以上是关于个人项目实战1,搭建springboot+mybatis框架项目的主要内容,如果未能解决你的问题,请参考以下文章
个人项目实战1,搭建springboot+mybatis框架项目
10分钟搭建 SpringBoot + Mybatis + Thymeleaf 个人博客
2019最新 Java商城秒杀系统的设计与实战视频教程(SpringBoot版)_2-1微服务项目的搭建-SpringBoot搭建多模块项目一