mybatis的概念:
MyBatis是一款优秀的持久层框架,支持定制化SQL、存储过程。
MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。
MyBatis可以使用简单的XML或注解来配置和映射原生信息,将接口和Java的POJOs映射成数据库中的记录。
MyBatis原是apache的一个开源项目iBatis;2010年该项目迁移到google code,改名为MyBatis。
原理结构图
Mybatis VS Hibernate
环境搭建:
IDEA,maven工程步骤
1.创建工程导入坐标
2.创建实体类和dao接口
3.创建mybatis的主配置文件 mybatis.xml
4.创建映射文件UserMapper.xml
环境搭建注意事项:
1.映射文件名UserMapper.xml和UserMapper.java名必须一样
2.在idea中创建目录的时候,它和包是不一样的
3.mybatis的映射配置文件位置必须和dao接口的包结构相同
4.映射配置文件的mapper标签,namespace属性的取值必须是dao接口的全限定类名
5.映射配置文件的操作配置(select),id属性必须是dao接口的发给发名
遵从了3.4.5可以无需写dao层
代码示例
1.pom配置文件
<packaging>jar</packaging> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
2.mybais.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属性要在文件最上面--> <settings> <setting name="mapUnderscoreToCamelCase" value="true" /> </settings> <!-- 运行环境配置(整合后归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://127.0.0.1:3306/mybatis" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <!-- 映射文件 --> <mappers> <mapper resource="com/dao/UserMapper.xml" /> </mappers> </configuration>
3.mapper.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.dao.UserMapper"> <select id="findAll" resultType="com.bean.User"> select *from test_user; </select> </mapper>
4.实体类
package com.bean;
public class User {
private Integer uId;
private String uName;
private String uLovel;
@Override
public String toString() {
return "User{" +
"uId=" + uId +
", uName='" + uName + '\'' +
", uLovel='" + uLovel + '\'' +
'}';
}
public Integer getuId() {
return uId;
}
public void setuId(Integer uId) {
this.uId = uId;
}
public String getuName() {
return uName;
}
public void setuName(String uName) {
this.uName = uName;
}
public String getuLovel() {
return uLovel;
}
public void setuLovel(String uLovel) {
this.uLovel = uLovel;
}
}
5.层接口类
package com.dao; import com.bean.User; import java.util.List; public interface UserMapper { List<User> findAll(); }
6.测试类
package com; import com.bean.User; import com.dao.UserMapper; 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.Test; import java.io.InputStream; import java.util.List; public class MybaitsTest01 { @Test public void m1() throws Exception{ //1.读取主配置文件 InputStream is= Resources.getResourceAsStream("mybatis.xml"); //2.创建SessionFactory工厂 SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(is); //3.使用工厂得到SQLSession对象 SqlSession session = factory.openSession(); //4.使用SqlSession创建dao接口的代理对象 UserMapper mapper = session.getMapper(UserMapper.class); //5.使用带库对象执行方法 List<User> all = mapper.findAll(); for (User user : all) { System.out.println(user); } //6.释放资源 session.close(); is.close(); } }
7.loj4.perporties属性文件
log4j.rootLogger=DEBUG, stdout #\u8F93\u51FA\u5230\u63A7\u5236\u53F0 log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n