Dowemo

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;



//省略相应的GetSet、toString、构造方法、



}






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"-//mybatis.org//DTD Config 3.0//EN"


"http://mybatis.org/dtd/mybatis-3-config.dtd">



<configuration>



 <!-- settings元素下是一些高级选项,可以改变mybatis运行中的行为。非必须 -->


 <settings>


 <!-- 开启数据库中下划线命名字段对java类中驼峰命名的映射关系。如:book_name与bookName互相映射 -->


 <setting name="mapUnderscoreToCamelCase" value="true"/>


 </settings>



 <!-- 给长字符串起一个别称。非必须 -->


 <typeAliases>


 <typeAlias alias="book" type="com.lxy.beans.Book"/>


 </typeAliases>



 <!-- 数据库连接环境。这里使用的是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"/>


 </dataSource>


 </environment>


 </environments>



 <!-- mappers中列出所有定义的mapper文件 -->


 <mappers>


 <mapper resource="com/lxy/mappers/BookMapper.xml"/>


 </mappers>


</configuration>






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"-//mybatis.org//DTD Mapper 3.0//EN"


"http://mybatis.org/dtd/mybatis-3-mapper.dtd">



<!-- 命名空间 -->


<mapper namespace="com.lxy.mappers.BookMapper">



 <!-- 简单查询方法 -->


 <select id="selectAll" resultType="book">


 select * from book;


 </select>



 <!-- 新增方法 -->


 <insert id="insertBook" parameterType="book">


 insert book (book_name,price) values(#{bookName},#{price});


 </insert>



</mapper>






test method


package com.lxy.test;



import java.io.IOException;


import java.io.InputStream;


import java.util.List;



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 com.lxy.beans.Book;



public class UserTest {



 public static void main(String[] args) throws IOException {


//SqlSessionFactory的作用域应为整个应用的生命周期。可以使用单例实现,但通常通过spring的依赖注入实现


 InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");


 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);



//SqlSession的作用域应在线程内。给每个request重新开启一个session。


//sqlsession有selectOne,selectList,insert,update,delete,commit,rollback等方法


 SqlSession session = factory.openSession();


 try {


 Book b = new Book("java进阶", 36.7d);


 session.insert("com.lxy.mappers.BookMapper.insertBook", b);


 session.commit();



 List<Book> lst = session.selectList("com.lxy.mappers.BookMapper.selectAll");


 for (Book book : lst) {


 System.out.println(book);


 }


 } finally {


//每次会话结束后关闭session


 session.close();


 }



 }


}











Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs