0 0 0 0

Mybatis is an excellent framework supporting common sql queries, stored procedures, and advanced maps.

The mybatis encapsulates jdbc and provides object mapping. He reduces almost all of the jdbc code and manual configuration, which simply requires simple xml or annotation configuration, which can perform the database operation.

Mybatis and spring combination can play a more powerful role.

There are two ways to use the Configure via xml profile, or configure through java annotations. In actual project development, it's flexible to switch between two ways. This article focuses on how xml is configured.

Here's a simple case to demonstrate how to use the mybatis.

this project environment: JDK1.8, mybatis3.3.0, mysql.

Create new java project, import mybatis mysql JDBCThe jar package ( if you want to use a log for functionality, also import other packages ). Figure:

Create a book table in the database, with id, title, price three fields, and I & ert several test data:

Create a bean class that corresponds to a database in a java project:

public class Book {
 private int id;
 private String bookName;
 private double price;

Here, you're ready to work. The mybatis task is to query the data we need from the database and map to the bean class. Or reverse the bean class insert update to the database.


The mybatis is the basic profile of the mybatis, and you need to configure the jdbc connection, reference all mapper files.

Create new project Mybatis-config. Xml. Profile:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
 PUBLIC"-// Config 3.0//EN"
 <!-- settings元素下是一些高级选项,可以改变mybatis运行中的行为。非必须 -->
 <!-- 开启数据库中下划线命名字段对java类中驼峰命名的映射关系。如:book_name与bookName互相映射 -->
 <setting name="mapUnderscoreToCamelCase" value="true"/>
 <!-- 给长字符串起一个别称。非必须 -->
 <typeAlias alias="book" type="com.lxy.beans.Book"/>
 <!-- 数据库连接环境。这里使用的是mysql的jdbc,事务管理及连接池也是用jdbc。 -->
 <!-- 这部分可由spring管理 -->
 <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/myschool"/>
 <property name="username" value="root"/>
 <property name="password" value="111112"/>
 <!-- mappers中列出所有定义的mapper文件 -->
 <mapper resource="com/lxy/mappers/BookMapper.xml"/>

to create mapper file

A mapper file usually corresponds to a bean class, which defines the mapping relationship between the data table and the java bean class, and the sql statement to execute.

Here you create a bookmapper. Xml and define two methods:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
 PUBLIC"-// Mapper 3.0//EN"
<!-- 命名空间 -->
<mapper namespace="com.lxy.mappers.BookMapper">
 <!-- 简单查询方法 -->
 <select id="selectAll" resultType="book">
 select * from book;
 <!-- 新增方法 -->
 <insert id="insertBook" parameterType="book">
 insert book (book_name,price) values(#{bookName},#{price});

test method

package com.lxy.test;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.lxy.beans.Book;
public class UserTest {
 public static void main(String[] args) throws IOException {
 InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
 SqlSession session = factory.openSession();
 try {
 Book b = new Book("java进阶", 36.7d);
 session.insert("com.lxy.mappers.BookMapper.insertBook", b);
 List<Book> lst = session.selectList("com.lxy.mappers.BookMapper.selectAll");
 for (Book book : lst) {
 } finally {

Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs