1、是什么
持久层框架
2、开发流程
(1)Mybatis根据配置文件创建会话工厂SqlSessionFactory
(2)利用SqlSessionFactory创建会话SqlSession
(3)利用SqlSession完成对数据的怎删改查和事务提交等操作
3、Mybatis和Hibernate的比较
Mybatis专注sql本身,程序员需要编写sql语句,所以sql修改和优化比较方便。Mybatis是一个不完全的ORM框架,Mybatis可以将输入参数自动进行输入映射,将查询结果集映射成java对象完成输出映射。适用于需求变化较多的项目,例如互联网项目。
Hibernate是一个标准的ORM框架,入门门槛较高,不需要程序员编写sql语句,sql语句自动生成,对sql修改和优化比较困难。适用于需求变化不多的项目,例如后台管理系统。
4、实例
(1)User.java
public class User {
private Integer id;
private String name;
private String password;
//get和set方法
}
(2)UserMapper.xml
<!-- 命名空间namespace作用就是对sql进行分类化管理 -->
<mapper namespace="test">
<!--
id标识映射文件中的sql
parameterType指定输入参数类型
resultType指定输出结果所映射的java对象类型
#{}表示一个占位符号,#{id}其中的id表示接收输入的参数的名称,如果输入参数是简单类型,#{}的参数名称可以任意
-->
<select id="findUserById" parameterType="int" resultType="com.mybatis.bean.User">
select * from user where id=#{id}
</select>
<!--
${}表示拼接符号
${}方式会引发SQL注入的问题,所以不建议使用
${value},接收输入参数的内容,如果传入参数是简单类型,${value}中只能使用value
-->
<select id="findUserByName" parameterType="java.lang.String" resultType="com.mybatis.bean.User">
select * from user where name like '%${value}%'
</select>
<!--
parameterType指定输入参数类型是pojo
#{}中指定pojo属性名
-->
<insert id="insertUser" parameterType="com.mybatis.bean.User">
insert into user(id,name,password) value(#{id},#{name},#{password})
</insert>
<!-- parameterType指定user对象,包括id和更新信息,注意id必须存在 -->
<update id="updateUserById" parameterType="com.mybatis.bean.User">
update user set name=#{name},password=#{password} where id=#{id}
</update>
<delete id="deleteUserById" parameterType="java.lang.Integer">
delete from user where id=#{id}
</delete>
</mapper>
(3)mybatis-config.xml
<configuration>
<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://localhost:3306/hwd"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/mybatis/mapper/UserMapper.xml"/>
</mappers>
</configuration>
(4)Test.java
public class Test {
@Before
public void setUp() throws Exception {
}
@org.junit.Test
public void findUserById() throws IOException {
//mybatis配置文件
String resource = "config/mybatis-config.xml";
//得到配置文件流
InputStream inputStream = Resources.getResourceAsStream(resource);
//创建会话工厂
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
//通过会话工厂得到SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
String statement = "test.findUserById";
//通过SqlSession操作数据库
//第一个参数:=命名空间+statement的id
//第二个参数:指定和映射文件中所匹配的parameterType类型的
User user = sqlSession.selectOne(statement, 1);
System.out.println(user.toString());
//关闭会话
sqlSession.close();
}
@org.junit.Test
public void findUserByName() throws IOException {
String resource = "config/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
String statement = "test.findUserByName";
List<User> users = sqlSession.selectList(statement, "小明");
System.out.println(users.toString());
sqlSession.close();
}
@org.junit.Test
public void insertUser() throws IOException {
String resource = "config/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
String statement = "test.insertUser";
User user = new User();
user.setId(3);
user.setName("黄小红");
user.setPassword("789");
sqlSession.insert(statement, user);
//提交事务
sqlSession.commit();
sqlSession.close();
}
@org.junit.Test
public void updateUserById() throws IOException {
String resource = "config/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
String statement = "test.updateUserById";
User user = new User();
user.setId(3);
user.setName("小红");
user.setPassword("777");
sqlSession.update(statement, user);
sqlSession.commit();
sqlSession.close();
}
@org.junit.Test
public void deleteUserById() throws IOException {
String resource = "config/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
String statement = "test.deleteUserById";
sqlSession.delete(statement, 3);
sqlSession.commit();
sqlSession.close();
}
}