Mybatis环境搭建
- (1)mybatis下载
mybaits的代码由github.com管理
下载地址:https://github.com/mybatis/mybatis-3/releases
- (2)导入jar包
测试相关的包: junit hamcrest
日志相关的包:log4j-1.2.16 slf4-api slf4j-log4j12
数据库相关jar包: mysql-connector
Mybatis先关jar包: mybaits-3.4.5
<dependencies>
<!-- 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)导入jar包log4j.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
Mybatis的操作步骤
(1)mybatis开发流程
》1 创建SqlSessionFactoryBuilder对象
》2 创建SqlSessionFactory对象
》3 加载SqlMapConfig.xml配置文件
》4 创建SqlSession对象
》5 执行SqlSession对象执行删除
》6 打印结果
》7 释放资源
Mybatis的入门案例-Java代码编写
- (1)先获取Session
- (2)再进行增删改查操作
src\test\java\com\wzx\pack01\Test01Myabatis.java
public class Test01Myabatis {
@Test
public void test01(){
// 》1 创建SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 》2 创建SqlSessionFactory对象
InputStream inputStream = Test01Myabatis.class.getClassLoader().getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sessionFactory = sqlSessionFactoryBuilder.build(inputStream);//加载核心配置文件 参1 输入流
// 》3 加载SqlMapConfig.xml配置文件
// 》4 创建SqlSession对象
SqlSession sqlSession = sessionFactory.openSession();
// 》5 执行SqlSession对象执行删除
System.out.println(sqlSession);
//delete from user where id = ?
sqlSession.delete("com.wzx.bean.User.deleteById",24);//执行删除 参1 是namespace.id 参2 参数值
// 》6 打印结果
// 》7 释放资源
sqlSession.commit();//mybatis的session不会自动提交,必须手动提交
sqlSession.close();
}
}
核心 配置文件
src\main\resources\SqlMapConfig.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">
<!--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:3306/mybatis01?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 配置多个映射文件-->
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
映射文件
src\main\resources\UserMapper.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">
<!-- map的约束文件-->
<!-- namespace 命名空间 防止id冲突找不到正确的id-->
<!-- id Mybatis是将sql语句写在xml文件中,以后session根据namesapce.id 合在一起获取sql-->
<!-- 在jdbc中,sql语句使用?作占位符,但在mybatis #{id}-->
<mapper namespace="com.wzx.bean.User">
<delete id="deleteById" parameterType="int" >
delete from user where id = #{id}
</delete>
</mapper>
封装-MySessionUtil ***
- (1)静态代码块
- (2)getSession方法
src\main\java\com\wzx\util\MySessionUtils.java
//Session工具类
public class MySessionUtils {
private static SqlSessionFactory sessionFactory;
//static 静态代码,在类加载的时候执行一次,且只执行一次
static{
// 》1 创建SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 》2 创建SqlSessionFactory对象
InputStream inputStream = MySessionUtils.class.getClassLoader().getResourceAsStream("SqlMapConfig.xml");
sessionFactory = sqlSessionFactoryBuilder.build(inputStream);//加载核心配置文件 参1 输入流
// 》3 加载SqlMapConfig.xml配置文件
}
public static SqlSession getSession() {
// 》4 创建SqlSession对象
SqlSession sqlSession = sessionFactory.openSession();
return sqlSession;
}
}
src\test\java\com\wzx\pack01\Test02Myabatis.java
public class Test02Myabatis {
@Test
public void test01(){
SqlSession sqlSession = MySessionUtils.getSession();
// 》5 执行SqlSession对象执行删除
System.out.println(sqlSession);
//delete from user where id = ?
sqlSession.delete("com.wzx.bean.User.deleteById",24);//执行删除 参1 是namespace.id 参2 参数值
// 》6 打印结果
// 》7 释放资源
sqlSession.commit();//mybatis的session不会自动提交,必须手动提交
sqlSession.close();
}
}