MyBatis框架环境搭建以及开发流程

Posted You295

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis框架环境搭建以及开发流程相关的知识,希望对你有一定的参考价值。

MyBatis框架环境搭建以及开发流程

一:maven项目的构建

1.下载软件,将其压缩包放在根目录下
2.创建文件夹maven-respository:依赖仓库,用来后序存放架包
2.conf文件夹里的配置:settings.xml文件配置
1)打开文件,将53行语句拷贝到55行位置,并且输入maven-respository文件的路径

2)160行配置mirror镜像(使用阿里云镜像)

3)配置jdk版本号:将profile,从201-217行剪切到237行位置

3.sts(eclipse)下配置maven
1)新建maven-workspace文件夹

2)sts切换路径

3)配置sts


4)javase项目的创建


项目结构说明:

5)javaee项目的创建


创建完成后,选中项目单击右键–进入Properties

接着:



二:mybatis开发流程

1.创建项目

2.配置依赖,在pom.xml中配置坐标

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dyit.mybatis</groupId>
  <artifactId>mybatis-hello</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>mybatis-hello</name>
  
  
  <dependencies>
  
  <!-- 和数据库关联所用架包 -->
     <dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.25</version>
		</dependency>
  
     <!-- 数据源 -->
     <dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.2.6</version>
		</dependency>
		
		<!-- mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.5.7</version>
		</dependency>
  
       <!-- 日志 -->
         <dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		
		<!-- lombok -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.20</version>
		</dependency>
		
		<!-- junit单元测试 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		
		<!-- 日志的包装 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.32</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.32</version>
		</dependency>
		
  </dependencies>
  
  
</project>

3.构建映射表的实体类(此时在数据库中创建了 book_db表 )

package com.dyit.mybatis.entity;

import org.apache.ibatis.type.Alias;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Alias("Book") //实体类的别名
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book 
    private Integer id;
    private String name;
    private Double price;
    private String author;


4.规范数据库的操作(定义接口为了实现 CRUD)

package com.dyit.mybatis.mapper;

import java.util.List;

import com.dyit.mybatis.entity.Book;

public interface IBookMapper 
    void save(Book book); //添加
    void delet(int id); //删除
    void upDate(Book book); //修改
    List<Book> findAll(); //全查询


5.配置框架
1)配置数据库相关的信息
druid:

druid.url=jdbc:mysql://192.168.1.8:3306/ssm_db?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
druid.username=root
druid.password=123
druid.driver=com.mysql.cj.jdbc.Driver

2)配置表和类的映射
MybatisDataSource:

package com.dyit.mybatis.until;

import org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory;

import com.alibaba.druid.pool.DruidDataSource;


/**
 * 使用自定义数据源
 * @author DELL
 *
 */
public class MybatisDataSource extends UnpooledDataSourceFactory 
    
	public MybatisDataSource() 
		this.dataSource = new DruidDataSource();
	


mybatis-configuration.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

	<!-- druid的配置文件的位置 ,$druid.username取出配置文件druid.username的值-->
	<properties resource="mybatis/druid/druid.properties"/>
	
	<!-- 配置别名,配合@Alais,扫描指定包内的锁有@Alais注解的类 -->
	<typeAliases>
		<package name="com.dyit.mybatis.entity"/>
	</typeAliases>

	<!--数据库的配置信息-->
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" /><!--JDBC事务管理-->
			<dataSource type="com.dyit.mybatis.util.MybatisDataSource" > <!---数据源-->
				<property name="driverClassName" value="$druid.driver" />
				<property name="url" value="$druid.url" />
				<property name="username" value="$druid.username" />
				<property name="password" value="$druid.password" />
			</dataSource>
		</environment>
	</environments>

	<!--配置表和类的映射文件-->
	<mappers>
		<mapper resource="mybatis/mapper/Book.mapper.xml" />
	</mappers>
</configuration>

3)配置CRUD:sql

配置namespace:

<!-- 接口的全名 -->
<mapper namespace="com.dyit.mybatis.mapper.IBookMapper">

</mapper>

配置表和类的映射关系:

<!-- 配置类的属性和表中的字段的映射关系 -->
	<resultMap type="Book" id="BookMap">
		<!-- 配置主键 -->
		<id property="id" column="book_id" />
		<result property="name" column="book_name" />
		<result property="price" column="book_price" />
		<result property="author" column="book_author" />
	</resultMap>

配置CRUD语句块:

  <!--添加  -->
	<insert id="save">
		INSERT INTO book_tab(book_name,book_price,
		book_author)
		VALUES(#name,#price,#author)
	</insert>

   <!-- 根据id删除 -->
	<delete id="delet">
		DELETE FROM book_tab WHERE book_id=#id
	</delete>
	
	 <!-- 修改信息 -->
	<update id="upDate">
		UPDATE book_tab SET book_name=#name, book_price=#price,book_author=#author 
		WHERE book_id=#id
	</update>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="BookMap">
		SELECT  * FROM book_tab
	</select>

6.代码开发测试:

package com.dyit.test;

import static org.junit.Assert.*;

import java.io.IOException;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.dyit.mybatis.entity.Book;
import com.dyit.mybatis.mapper.IBookMapper;

public class MybatisTest 
	
	private SqlSession sqlSession;
	private static SqlSessionFactory sqlFactory; //创建工厂

	static 
		try 
			sqlFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis/config/mybatis-configuration.xml"));
			
		 catch (IOException e) 
			e.printStackTrace();
		
	
	
	@Before
	 public void before() 
		sqlSession = sqlFactory.openSession(); //打开session一遍于后边的CURD操作
	
	
	
	
	@Test
	public void testSave()  //添加
		IBookMapper mapper = sqlSession.getMapper(IBookMapper.class);
		mapper.save(new Book(null, "爱情来了", 55.98, "艺人"));
	
	
	@Test
	public void testUpDate()   //修改
		IBookMapper mapper = sqlSession.getMapper(IBookMapper.class);
		mapper.save(new Book(1, "天天开心", 55.09, "诗人"));
		
	
	
	
	@Test
	public void testDelet()   //删除
		IBookMapper mapper = sqlSession.getMapper(IBookMapper.class); //获取接口路径
		mapper.delet(1);
	
	
	
	@Test
	public void TestfindAll()  //查找
		IBookMapper mapper = sqlSession.getMapper(IBookMapper.class);
		List<Book> list = mapper.findAll();
		System.out.println(list);
	
	@After  //最后执行事务的提交和关闭连接
	public void after() 
		sqlSession.commit();
		sqlSession.close();
	



以上是关于MyBatis框架环境搭建以及开发流程的主要内容,如果未能解决你的问题,请参考以下文章

mybatis实战教程(mybatis in action)之一:开发环境搭建

MyBatis环境搭建

springmvc+mybatis+oracle

Mybatis入门级学习-环境搭建

MyBatis——环境搭建

基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建