Mybatis 环境搭建
1.下载mybatis
在github上下载
2.导入依赖
测试相关的包: junit hamcrest
日志相关的包:log4j-1.2.16 slf4-api slf4j-log4j12
数据库相关jar包: mysql-connector
Mybatis先关jar包: mybaits-3.4.5
<!-- junit--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!-- MySql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.32</version></dependency><!-- Mybatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.5</version></dependency><!--日志包--><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.25</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency></dependencies>
3.创建log4j文件
(1)首先创建resources包(src\main\resources
)
(2)在resources包下创建log4j.properties文件
(3)配置log4j.properties
# Global logging configurationlog4j.rootLogger=DEBUG, stdout# Console output...log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
2.Mybatis操作流程
(1)mybatis开发流程
》1 创建SqlSessionFactoryBuilder对象
》2 创建SqlSessionFactory对象
》3 加载SqlMapConfig.xml配置文件
》4 创建SqlSession对象
》5 执行SqlSession对象执行删除
》6 打印结果
》7 释放资源
详细代码
//创建SqlSessionFactoryBuilder对象SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();//创建SqlSessionFactor对象InputStream inputStream=TestMybatis01.class.getClassLoader().getResourceAsStream("SqlMapConfig.xml");SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);//加载文件,输入流//创建SqlSession对象SqlSession sqlSession=sqlSessionFactory.openSession();System.out.println(sqlSession);//执行sql语句User user=new User();user.setId(8);user.setName("xjj");sqlSession.insert("com.xjj.bean.User.insetUser",user);mit();//mybatis的session不会自动提交,必须手动提交sqlSession.close();
mybatis核心配置文件
(1)创建SqlMapConfig.xml文件
在src\main\resources下
,创建方法和log4j相同
(2)配置SqlMapConfig.xml文件
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-////DTD Config 3.0//EN""/dtd/mybatis-3-config.dtd"><!--dtd是对xml配置标签与属性的约束--><configuration><!-- environments环境 开发环境,生产环境--><environments default="development"><environment id="development"><!-- 使用jdbc的事务管理 --><transactionManager type="JDBC"/><!-- dataSource Mybatis自带数据源连接池--><dataSource type="POOLED"><!-- 四大信息 --><property name="driver" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost/user?characterEncoding=utf-8"/><property name="username" value="root"/><property name="password" value="123456"/></dataSource></environment></environments><!-- 配置多个映射文件--><mappers><mapper resource="UserMapper.xml"/></mappers></configuration>
(3)映射文件
在src\main\resources下创建UserMapper.xml文件
创建方法和上述两个文件相同
(4)配置映射文件
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-////DTD Mapper 3.0//EN""/dtd/mybatis-3-mapper.dtd"><!-- map的约束文件--><!-- namespace 命名空间 防止id冲突找不到正确的id--><!-- id Mybatis是将sql语句写在xml文件中,以后session根据namesapce.id 合在一起获取sql--><!-- 在jdbc中,sql语句使用?作占位符,但在mybatis #{id}--><mapper namespace="com.xjj.bean.User"><delete id="deleteById" parameterType="int" >delete from user where id = #{id}</delete><insert id="insetUser" >insert into user (id,name) values (#{id},#{name})</insert><select id="findUserById" resultType="com.xjj.bean.User" >select * from user where name = #{name}</select></mapper>
MySessionUtil
1.静态代码快
2.getSession()方法
创建MySessionUtil.java
src\main\com\xjj\util\MySessionUtil.java
package com.xjj.util;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.InputStream;public class MysqlSessionUtil {private static SqlSessionFactory sessionFactory;//static 静态代码只会执行一次static{// 创建SqLSessionFactoryBuilder对象SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();//创建SqlSessionFactory对象InputStream inputStream =MysqlSessionUtil.class.getClassLoader().getResourceAsStream("SqlMapConfig.xml");sessionFactory=sqlSessionFactoryBuilder.build(inputStream);//参1 输入流,加载SqlMapConfig.xml文件}public static SqlSession getSession(){//创建SqlSession对象SqlSession sqlSession=sessionFactory.openSession();return sqlSession;}}
创建TestMybatis02()文件
src\main\test\java\com\xjj\pack01\TestMybatis02
package com.xjj.pack01;import com.xjj.util.MysqlSessionUtil;import org.apache.ibatis.session.SqlSession;import org.junit.Test;public class TestMybatis02 {@Testpublic void test01(){//创建Sqlsession对象,调用getSession方法SqlSession sqlSession = MysqlSessionUtil.getSession();System.out.println(sqlSession);sqlSession.delete("com.xjj.bean.User.deleteById",1);//删除id编号为1的user的所有信息mit();//mybatis不会自动提交sessionsqlSession.close();//释放资源}}