Mybatis入门教程

版权声明:本文为博主原创文章,版权归原作者小思所有,转载或者引用本文内容请注明来源及原作者 https://blog.csdn.net/zeal9s/article/details/83472367

本项目是建立在Maven项目基础上的,也可以建立java项目, mybatis-x.x.x.jar 下载Jar包,将项目所需Jar包拷贝到项目中即可

项目板块图
在这里插入图片描述
1.maven在线搜索maven中Mybatis的依赖,将依赖拷贝到pom.xml文件中,并保存(小编第一次下载没有成功,去maven的本地仓库删除再重新保存pom.xml下载一次)

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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>Mybatis_01</groupId>
	<artifactId>Mybatis_01</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>Mybatis_01 Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<!--使用maven的单元测试版 -->
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<!--引入Mybatis依赖 -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.1</version>
		</dependency>
		<!--引入MySql依赖 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.43</version>
		</dependency>

	</dependencies>
	<build>
		<finalName>Mybatis_01</finalName>
	</build>
</project>

2.在resource目录下新建mybatis-config.xml

此文件是Mybatis的核心配置文件,包括其使用的数据源和事务管理和映射

<?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>
	<!--配置Author.xml里面的返回类型 -->
	<typeAliases>
        <package name="com.mybatis.entity"/>
	</typeAliases>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<!--配置获取数据库连接实例的数据源 -->
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver" />
				<property name="url"
					value="jdbc:mysql://localhost:3306/zs?characterEncoding=utf-8" />
				<property name="username" value="root" />
				<property name="password" value="1234" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="com/mybatis/entity/Author.xml" />
	</mappers>
</configuration>

3.新建com.mybatis.entity包,并在其目录下新建Author.java(实体类)
Author.java

package com.mybatis.entity;
/**
 * 
* @ClassName: Author
* @Description:作者实体类
* @author 小思
* @date 2018年10月27日 下午8:23:27
*
 */
public class Author {
	
	private Integer aid;//作者id
	private String aname;//作者姓名
	
	public Integer getAid() {
		return aid;
	}
	public void setAid(Integer aid) {
		this.aid = aid;
	}
	public String getAname() {
		return aname;
	}
	public void setAname(String aname) {
		this.aname = aname;
	}
	public Author(Integer aid, String aname) {
		super();
		this.aid = aid;
		this.aname = aname;
	}
	public Author(String aname) {
		super();
		this.aname = aname;
	}
	public Author() {
		super();
	}
	@Override
	public String toString() {
		return "Author [aid=" + aid + ", aname=" + aname + "]";
	}
	
	
	
	

}

4.新建com.mybatis.dao包,并在其目录下新建AuthorDao.java(映射接口)

AuthorDao.java

package com.mybatis.dao;
/**
 * 
* @ClassName: AuthorDao
* @Description:作者类的dao方法的接口,合理描述方法的参数和返回值的接口,方法名映射Author.xml里面的id
* @author 小思
* @date 2018年10月27日 下午8:27:09
*
 */

import java.util.List;

import com.mybatis.entity.Author;

public interface AuthorDao {
	/**
	 * 
	* @Title: getAll
	* @Description:查询所有的作者
	* @return
	* @return List<Author>
	 */
	public List<Author> getAll();
	
	/**
	 * 
	* @Title: findAuthorById
	* @Description:通过作者的Id查找作者
	* @return
	* @return Author
	 */
	public Author findAuthorById(int aid);
	
	/**
	 * 
	* @Title: addAuthor
	* @Description:添加作者
	* @param author
	* @return
	* @return Author
	 */
	public void addAuthor(Author author);
	
	/**
	 * 
	* @Title: updAuthor
	* @Description:修改作者
	* @param author
	* @return
	* @return void
	 */
	public void updAuthor(Author author);
	
	/**
	 * 
	* @Title: delAuthor
	* @Description:删除作者
	* @param aid
	* @return void
	 */
	public void delAuthor(int aid);

}

5.在com.mybatis.entity包的目录下新建Author.xml(数据库操作和java方法的映射文件)
Author.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.mybatis.dao.AuthorDao">
  <!--下列的数据库操作,通过id和返回类型,会映射AuthorDao接口中的方法  -->
  <select id="getAll" resultType="Author">
    select * from Author
  </select>
  <select id="findAuthorById" resultType="Author" parameterType="int">
    select * from Author where aid=#{aid}
  </select>
  <insert id="addAuthor" parameterType="Author">
    insert into author(aname) values(#{aname})
  </insert>
  <update id="updAuthor" parameterType="Author">
    update author set aname=#{aname} where aid=#{aid}
  </update>
  <delete id="delAuthor" parameterType="int">
    delete from author where aid=#{aid}
  </delete>
</mapper>

6.在src/test/java目录下新建com.mybatis包,并在其目录下新建TestMybatis.java(单元测试类)

package com.mybatis;

import java.util.List;

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.mybatis.dao.AuthorDao;
import com.mybatis.entity.Author;

public class TestMybatis {
	private SqlSessionFactory sessionFactory;
	private SqlSession session;

	@Before
	public void before() {
		// 通过获取对象当前的类类型获取当前类型的类加载器来加载类类路径下的资源文件以此构建SqlSessionFactory
		sessionFactory = new SqlSessionFactoryBuilder()
				.build(getClass().getClassLoader().getResourceAsStream("mybatis-config.xml"));
		// 创建SqlSession实例来自行数据库的所有方法和sql语句
		session = sessionFactory.openSession();
	}

	@Test
	public void test() {
		// 映射器是一个你创建来绑定你映射的语句的接口,调用合理描述参数和返回值的接口来获取相应的方法
		AuthorDao authorDao = session.getMapper(AuthorDao.class);
//		// 查询全部的作者
//		List<Author> list = authorDao.getAll();
//		for (Author author : list) {
//			System.out.println(author);
//		}

//		// 通过作者的Id查找作者
//		Author author=authorDao.findAuthorById(1);
//		System.out.println(author);
		
//		//添加作者
//		Author author=new Author("王麻子");
//		authorDao.addAuthor(author);
//		System.out.println("添加成功");
		
//        //修改作者
//		Author author=new Author(1,"王六");
//		authorDao.updAuthor(author);
//		System.out.println("修改成功");
		
		//删除作者
		authorDao.delAuthor(4);
		System.out.println("删除成功");

	}

	@After
	public void after() {
		// 保持SqlSession 的实例是线程安全的,使用完之后关闭sqlSession数据库资源
		session.commit();
		session.close();

	}

}

此博文是最基础的Mybatis项目应用,基础请查看官网了解更多使用与操作

此博文的具体项目和Mysql表格我已上传,需要的伙伴可以自行下载,下载地址

说在最后的话:编写实属不易,若喜欢或者对你有帮助记得点赞+关注或者收藏哦~

猜你喜欢

转载自blog.csdn.net/zeal9s/article/details/83472367
今日推荐