mybatis使用详解 Mybatis的详细使用教程
人气:0想了解Mybatis的详细使用教程的相关内容吗,在本文为您仔细讲解mybatis使用详解的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:mybatis使用详解,下面大家一起来学习吧。
Mybatis所需要的jar包:
需要引用两个jar包,一个是mybatis
,另一个是MySQL-connector-Java
,如果是maven工程的话,pom里如下添加依赖就可以了。
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.26</version> </dependency>
数据准备:
在MySQL中造点数据进行测试用:
CREATE DATABASE mybatis_test; CREATE TABLE user ( age INTEGER NOT NULL, name VARCHAR(64) NOT NULL DEFAULT '' ); insert user values(18,'zhanjindong'); insert user values(20,'zhangsan');
配置文件:
需要两类配置文件,一类是MyBatis的配置文件mybatis-config.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> <settings> <!-- changes from the defaults for testing --> <setting name="cacheEnabled" value="false" /> <setting name="useGeneratedKeys" value="true" /> <setting name="defaultExecutorType" value="REUSE" /> </settings> <typeAliases> <typeAlias alias="User" type="test.mybatis.User"/> </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://192.168.71.38:3306/mybatis_test"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mappers/UserMapper.xml" /> </mappers> </configuration>
另一类是数据访问接口映射文件:示例中是UserMapper.xml。这个文件一般放在src/main/resource下面或子目录下mybatis就可以找到了,在mybatis-config.xml中由mappers/mapper节点的resource指定。
<?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="test.mybatis.UserMapper"> <!-- 这里namespace必须是UserMapper接口的路径” --> <insert id="insertUser" parameterType="User"> insert into user(name,age) values(#{name},#{age}) <!-- 这里sql结尾不能加分号,否则报“ORA-00911”的错误 --> </insert> <!-- 这里的id必须和UserMapper接口中的接口方法名相同 --> <select id="getUser" resultType="User" parameterType="java.lang.String"> select * from user where name=#{name} </select> </mapper>
对应这个映射文件就是test.mybatis这个命名空间下的UserMapper这个接口,只是定义了访问数据表的接口:
package test.mybatis; public interface UserMapper { public void insertUser(User user); public User getUser(String name); }
需要一个POJO:User.java
package test.mybatis; public class User { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public User(String name, Integer age) { super(); this.name = name; this.age = age; } public User() { super(); } }
测试:
通过MyBatis操作数据库用到的是一个叫SqlSession的类,这个类是通过SqlSessionFactory产生的,一般建议在全局维护一个SqlSessionFactory就可以了。
TestMyBatis.java package test.mybatis; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MyBatisUtil { private final static SqlSessionFactory sqlSessionFactory; static { String resource = "mybatis-config.xml"; Reader reader = null; try { reader = Resources.getResourceAsReader(resource); } catch (IOException e) { System.out.println(e.getMessage()); } sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } public static SqlSessionFactory getSqlSessionFactory() { return sqlSessionFactory; } }
测试代码如下:
TestMyBatis.java package test.mybatis; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; public class TestMyBatis { static SqlSessionFactory sqlSessionFactory = null; static { sqlSessionFactory = MyBatisUtil.getSqlSessionFactory(); } public static void main(String[] args) { testAdd(); getUser(); } public static void testAdd() { SqlSession sqlSession = sqlSessionFactory.openSession(); try { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = new User("lisi", new Integer(25)); userMapper.insertUser(user); sqlSession.commit();// 这里一定要提交,不然数据进不去数据库中 } finally { sqlSession.close(); } } public static void getUser() { SqlSession sqlSession = sqlSessionFactory.openSession(); try { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = userMapper.getUser("zhangsan"); System.out.println("name: " + user.getName() + "|age: " + user.getAge()); } finally { sqlSession.close(); } } }
1、mybatis会用log4j记录日志,但是开启debug模式貌似对性能影响非常厉害。
2、mybatis的查询缓存对性能的影响非常大,启用和不启用差距非常大
以上所述是小编给大家介绍的Mybatis的详细使用教程,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
加载全部内容