今天咱们分享一些mybait的一些入门知识,希望对大家有帮助
Mybatis是Apache的一个Java开源项目,是一个支持动态Sql语句的持久层框架。Mybatis可以将Sql语句配置在XML文件中,避免将Sql语句硬编码在Java类中。与JDBC相比: 1)Mybatis通过参数映射方式,可以将参数灵活的配置在SQL语句中的配置文件中,避免在Java类中配置参数(JDBC) 2)Mybatis通过输出映射机制,将结果集的检索自动映射成相应的Java对象,避免对结果集手工检索(JDBC) 3)Mybatis可以通过Xml配置文件对数据库连接进行管理
SqlSessionFactoryBuilder: 根据配置信息生成SqlSessionFactory
SqlSessionFactory: 用于生成SqlSession SqlSession: SqlSession是MyBatis的关键对象,通过这个接口可以操作命令,管理事务等
SqlMapper:MyBatis的设计组件,有java接口和xml文件构成。需要给出对应的sql映射和映射规则
mybatis为一款ORM框架,作为对象关系映射模式的框架,主要为了解决面向对象与关系数据库存在互不匹配的现象,只要提供了持久化类与表的映射关系,ORM框架在运行时就能参照映射文件的信息,把对象持久化到数据库中,进行CRUD;
1) 使用maven新建一个web工程
点击下一步
2) idea在创建web工程时不会自动创建java,resources,test等目录
3)通过pom.xml添加必要的依赖
<properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <maven.compiler.source>1.8maven.compiler.source> <maven.compiler.target>1.8maven.compiler.target> properties> <dependencies> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.12version> <scope>testscope> dependency> <dependency> <groupId>javax.servletgroupId> <artifactId>javax.servlet-apiartifactId> <version>4.0.0version> <scope>providedscope> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>3.4.5version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>5.1.44version> dependency> <dependency> <groupId>com.belerwebgroupId> <artifactId>pinyin4jartifactId> <version>2.5.1version> dependency> <dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> <version>1.18.24version> <scope>providedscope> dependency> dependencies>4)下载两个插件,用与生成mybatis配置文件的
5)将mybatis核心配置文件(mybatis.cfg.xml),jdbc.properties, log4j.xml考到项目的resources目录下,并修改数据库的连接。
"1.0" encoding="UTF-8"?> configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="jdbc.properties"/> <environments default="development"> <environment id="development"> <transactionManager type="jdbc"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> dataSource> environment> environments> <mappers> <mapper resource="mapper/Iusermapper.xml"/> mappers> configuration>5.1)数据库连接
6) 在src/main/resources目录下创建mapper用于方式mybatis的映射文件
mapper位置
mybatis的映射文件内容
"1.0" encoding="UTF-8" ?> mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.zking.mybatis.mapper.Iusermapper"> <select id="listuser" resultType="com.zking.mybatis.model.User"> select id,name,loginName,pwd,rid from t_oa_user <where> <if test="name !=null and name !=''"> and name like concat(#{name} +'%') if> where> select> mapper>7).在目录结构中加入一个mapper包,在该包中创建一个Iusermapper接口
注意id要与方法名一致,查询需要返回类型resultType
7)测试之前要写个帮助类MybatisSessionFactoryUtils
public class MybatisSessionFactoryUtils { public static SqlSessionFactory sqlSessionFactory; private static ThreadLocalthreadLocal = new ThreadLocal (); static { try { InputStream is = MybatisSessionFactoryUtils.class .getResourceAsStream("/mybatis.cfg.xml"); SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); sqlSessionFactory = sqlSessionFactoryBuilder.build(is); } catch (Exception e) { throw new RuntimeException(e); } } private MybatisSessionFactoryUtils() { } public static SqlSession openSession() { SqlSession sqlSession = threadLocal.get(); if (null == sqlSession) { sqlSession = sqlSessionFactory.openSession(); threadLocal.set(sqlSession); } return sqlSession; } public static void closeSession() { SqlSession sqlSession = threadLocal.get(); if (null != sqlSession) { //threadLocal.set(null); threadLocal.remove(); sqlSession.close(); } }测试结果
测试包UserserviceimplTest
public class UserserviceimplTest { private Iuserservice userserviceimpl=new Userserviceimpl(); private SqlSession sqlSession; @Before public void setUp() throws Exception { sqlSession = MybatisSessionFactoryUtils.openSession(); Iusermapper mapper = sqlSession.getMapper(Iusermapper.class); userserviceimpl.setIusermapper(mapper); } @After public void tearDown() throws Exception { sqlSession.commit(); } @Test public void listuser(){ System.out.println(userserviceimpl.listuser(null)); }