【Springboot 入门培训 】#1 MyBatis项目运行环境配置

Springboot + MyBatis 入门培训

本文是作者在公司中培训应届刚刚入职的新员工时所使用和编写的培训入门教程。以新手快速入门为目的,没有太多大而空的理论套话内容,都是化繁为简的应用技巧。可以让新手快速掌握Springboot下的MyBatis开发与应用。
我在2005的时候第一次在项目中学习和使用了hibnate,那个时候非常的震撼世界上还有这种好东西,原来都是在jdbc封装bean的模式,代码都是大量的重复封装工作。但是在使用过hibnate一段时间后发现hibnate优化和应用上需要一些综合能力(例如对sql文优化),但是那时的自己还驾驭不了hibnate,用起来总是遇到各种大坑。这个时候2007左右IBatis很流行,那时我做对日外包,日本人的项目基本都是用IBatis,这个时候忽然感到IBatis比hibnate方便快捷多了,自己也能驾驭的了,用起来很开心。时间到了2010年左右那时候我已经做国内项目了也成长到一定职务级别,可以自己选型技术架构了,几年来使用IBatis在开发也发现他存在不少无法解决的问题和缺点,就重新选择了hibnate。通过几年的技术积累这个时候在用hibnate就不一样了,那真是如鱼得水用起来那真是一个快,开发进度快,代码维护方便,项目后期运维修改量也很小,真的是爱上了hibnate。如果要是开发者的能力可以驾驭了hibnate,那开发起来真是一个爽啊。但是后来我们技术选型为什么选择MyBatis了呢,不是因为MyBatis比hibnate优秀。是应为hibnate在项目开发中需要开发组中的开发人员都是2~3年左右的熟练开发人员。但是最近几年公司人员流动大(大家都懂大量开发人员从二三线城市转移到一线城市)我们这只能招聘到一些没用开发经验的应届学生和那些不是很优秀的开发人员,他们的能力是没有办法快速驾驭和使用hibnate。这时我们人员结构发生变化,我们只能调整了开发技术来保证开发稳定,MyBatis入门简单,上手快,几天的培训就可以进入到项目组中进行项目开发与维护,培训周期短。希望大家通过对作者发表的这一系列文章的学习,能快速掌握Spring boot加MyBatis来开发项目。

1 项目测试与运行环境配置

这个项目代码是在eclipse工具下运行,主要是针对那些刚刚入门的开发者对idea不太熟悉不能很快的创建项目,大部分的入门开发者都使用过eclipse,在eclipse下可以很快的调试代码。
源码下载地址链接:https://pan.baidu.com/s/1zQdKWYrxYZ_LjNfDHw8I5w
提取码:1234
下载springmybatis培训练习一.zip
本站下载:springmybatis培训练习

项目配置

在eclipse项目中导入要使用到的jar包,只有导入jar才可以正常运行项目。

在这里插入图片描述
查找到项目中jar所放位置,选择要导入的jar包后 点ok 导入jar包。
在这里插入图片描述
数据库表创建

CREATE TABLE `user` (
  `id` int(11) DEFAULT NULL,
  `user` varchar(255) COLLATE utf8_bin DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

如果没mysql数据库
可以下载作者的数据库,在百度网盘中下载数据库后直接解压,解压完成后进入mysql51文件夹中,启动数据库。在这里插入图片描述
https://pan.baidu.com/s/1zQdKWYrxYZ_LjNfDHw8I5w
提取码:1234
下载mysql51数据库
本站下载:mysql51数据库

项目结构

项目
  |--src
  |   |--cn.core.my.app
  |          |  |--OnApp       //容器启动类
  |          |  |--MyBatisTest //测试类 
  |          |--dao            //数据库操作接口
  |              |--UserDao    //数据操作类
  |--application.properties    //项目配置文件
  |--mapper
  |    |--UserSql.xml          //MyBatis数据库sql文文件 
  |--lib  //项目运行jar包

配置 application.properties 文件MyBatis设置内容

​ 在项目代码 src目录中找到spring boot 的项目配置文件,开发环境中properties 文件在src目录下,运行环境中在classes 目录中。

项目配置 application.properties 文件配置一下几个内容

  • 数据库连接池设置,使用的是阿里druid数据连接池
  • 数据库地址,用户名称,密码
  • java mysql数据驱动
  • mybatis 执行sql文件地址与文件命名规则
  • mybatis后台打印,在开发环境中为了方便调试看mybatis调用sql运行情况
#阿里数据连接池 
type=com.alibaba.druid.pool.DruidDataSource 
#数据库地址  用户名称  密码 
spring.datasource.url=jdbc:mysql://localhost:3306/systext?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
#数据库驱动  
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#MyBatis  sql文件地址设置  class目录下mapper文件夹下  ***.xml文件      
mybatis.mapperLocations=classpath*:/mapper/*.xml
#数据库后台打印日志
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

application.yml 配置方法

application.yml文件是树状结构分层的配置方法,层与层之间空格或者缩进,每层都要定义:符号表示层的设置内容。现在spring boot yml文件设置成为主流的配置方法之一。

  • druid 设置数据池信息基础配置
server:
  port: 8888
spring:
 datasource:
  url: jdbc:mysql://localhost:3307/systext?useUnicode=true&characterEncoding=UTF-8
  username: root
  password: 123456
  driver-class-name: com.mysql.jdbc.Driver
  type: com.alibaba.druid.pool.DruidDataSource
  druid:
   initialSize: 5
   minIdle: 5
   maxActive: 20
   maxWait: 60000
   timeBetweenEvictionRunsMillis: 60000
   minEvictableIdleTimeMillis: 300000
   testWhileIdle: true
   testOnBorrow: false
   testOnReturn: false
   poolPreparedStatements: true   
mybatis:
  mapperLocations: classpath*:/mapper/*.xml
  configuration: 
   log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

配置spring boot启动类

  • 我们使用spring boot 中的 SpringApplicationBuilder 类为spring 容器启动类来加载spring 中的所有容器类。
  • 将 spring boot 容器加载设置为不启动web功能,我们只用于测试MyBatis 数据库操作功能。
@SpringBootApplication
public class OnApp {
	public static void main(String[] args) {
        new SpringApplicationBuilder(OnApp.class).
            web(WebApplicationType.NONE).//设置为不启动web功能
            run(args);
	}	
}

设置MyBatis 执行sql语句的xml文件与接口

在项目中的application.properties 文件中,我们将MyBatis sql文执行文件设置在classes 目录中的mapper文件夹下,在开发环境中就是在src目录下的mapper文件中。

  • 在mapper文件夹下创建UserSql.xml文件
  • 创建sql文业务对应的spring 容器类
    < mapper namespace=spring容器UserDao 地址>
  • 在UserSql.xml中创建查询业务的sql文 < select >
  • 在spring 容器类中定义查询sql文的抽象方法
    UserDao 接口中 List UserList(Map map);

UserSql.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">
//建立spring 容器类与 MyBatis 业务关联
<mapper namespace="cn.core.my.app.dao.UserDao">
    //id="UserList" 抽象方法名称  resultType="map" sql参数类型 map类型
    <select id="UserList" resultType="map">
        select * from user
    </select>
</mapper>

创建Spring容器 UserDao

@Component
@Mapper
public interface UserDao {
	 List<Map> UserList(Map map);
}

创建MyBatis测试类运行sql文

在项目中创建测试类来运行select * from user这个业务的sql文内容。

  • @RunWith(SpringRunner.class) 加载Spring boot容器
  • @SpringBootTest(classes=OnApp.class) 加载启动Spring boot入口类
  • @Test 定义测试方法

测试类定义

@RunWith(SpringRunner.class)
@SpringBootTest(classes=OnApp.class)
public class MyBatisTest {
	@Autowired//引入业务接口类
	UserDao userdao;
	@Test
	public void TestUserList(){
		List<Map> list=userdao.UserList(new HashMap());
        for(Map map:list){
            System.out.println(map);
        }
	}
}

运行测试代码
在这里插入图片描述

在测试类中选择要测试的方法右键。

在这里插入图片描述

在eclipse中的运行JUnit测试,然后在控制台查看测试结果。

在这里插入图片描述

测试运行成功后,在控制台可以看到运行时候的sql文方便调试程序。

其他配置

如果用户使用的idea或者是mave创建的项目,请使用以下配置项目导入项目jar包。

pom.xml

<dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.1</version>
        </dependency>
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-web</artifactId>
		    <version>2.1.10.RELEASE</version>
		</dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>2.1.10.RELEASE</version>
        </dependency>		
  		<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <version>2.1.10.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
       <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.39</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>	
    </dependencies>

Springboot + MyBatis入门培训2 增改删除与查询 in like foreach操作
Springboot + MyBatis入门培训 3 多数据源与缓存和数据连接池设置

评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zht_bs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值