mybatis初学总结
2019年8月9日19:45:00
1.orm
什么是orm?
orm即对象关系映射 (object relation mapping),即java对象和关系数据表之间的映射关系,
java实体的名字 ----- 表名
java实体的名字 ------ 字段名
orm有哪些?
SpringJDBC,beanUtils,mybaties,hibernate
myBatis和hibernate的区别?
两者底层都用了jdbc去实现功能,都有数据库连接池,
不同之处:hibernate适应于项目代码(sql)基本不变的需求下时比较方便,myBatis的应用比较灵活
myBatis主要完成的工作?
1.底层jdbc数据库连接(所以mysql-connection包还是要导)
2.运用反射技术将java实体和数据库表产生映射关系,一一对应
(另外还内含数据库连接池,所以比c3p0+beanUtils强大)
2.如何配置mybatis在eclipse?
1.导包
2.配置mybatis-config.xml文件在src下
使用mybatis的注意事项
例子:
public class Demo01 {
public static void main(String[] args) throws IOException {
//selectAll();
//insertOne();
//selectOnebyId();
//updateOneById();
//delectByID();
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
List<Student> studentlist = sqlSession.selectList("com.xiaoxin.bean.StudentMapper.selectAll");
for (Student student : studentlist) {
System.out.println(student);
}
SqlSessionUtils.releaseSqlSession(sqlSession);
}
private static void delectByID() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession openSession = sqlSessionFactory.openSession();
openSession.delete("com.xiaoxin.bean.StudentMapper.deleteOneByid",2);
openSession.commit();
openSession.close();
}
private static void updateOneById() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession openSession = sqlSessionFactory.openSession();
Student stu = new Student(2,"小芳", 22);
openSession.update("com.xiaoxin.bean.StudentMapper.updateOneByid", stu);
openSession.commit();
openSession.close();
}
private static void selectOnebyId() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession openSession = sqlSessionFactory.openSession();
Student stu = openSession.selectOne("com.xiaoxin.bean.StudentMapper.selectOneById", 1);
System.out.println(stu);
openSession.commit();
openSession.close();
}
private static void selectAll() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession openSession = sqlSessionFactory.openSession();
List<Student> stulist = openSession.selectList("com.xiaoxin.bean.StudentMapper.selectAll");
for (Student student : stulist) {
System.out.println(student);
}
openSession.commit();
openSession.close();
}
private static void insertOne() throws IOException {
// ceshimap
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession openSession = sqlSessionFactory.openSession();
Student stu = new Student("小敏", 21);
openSession.insert("com.xiaoxin.bean.StudentMapper.saveOne", stu);
// 要手动提交
openSession.commit();
// 关闭opensession对象
openSession.close();
}
}
xml如何配置?
mybatis-config.xml(放src下)
<?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>
<!-- 配置MyBatis的环境 -->
<environments default="env">
<!-- 配置一个环境 -->
<environment id="env">
<!-- 配置事务管理器 JDBC(有事务)、MANAGED(托管) -->
<transactionManager type="JDBC" />
<!-- 配置数据源 JNDI(web服务器方式定义数据源)、POOLED(自带连接池)、UNPOOLED(不带连接池) -->
<dataSource type="POOLED">
<!-- 连接数据库驱动 -->
<property name="driver" value="com.mysql.jdbc.Driver" />
<!-- 连接数据库URL -->
<property name="url" value="jdbc:mysql://localhost:3306/mybatiesdemo1" />
<!-- 连接数据库用户名 -->
<property name="username" value="xiaoxin" />
<!-- 连接数据库密码 -->
<property name="password" value="root" />
</dataSource>
</environment>
<!-- 配置一个环境,这里可以是别的环境,可以是别的数据库 -->
<!-- <environment id="env">
配置事务管理器 JDBC(有事务)、MANAGED(托管)
<transactionManager type="POOLED" />
配置数据源 JNDI(web服务器方式定义数据源)、POOLED(自带连接池)、UNPOOLED(不带连接池)
<dataSource type="POOLED">
连接数据库驱动
<property name="driver" value="com.mysql.jdbc.Driver" />
连接数据库URL
<property name="url" value="jdbc:mysql://localhost:3306/aa" />
连接数据库用户名
<property name="username" value="root" />
连接数据库密码
<property name="password" value="root" />
</dataSource>
</environment> -->
</environments>
<!-- 配置XxxMapper.xml访问数据库SQL文件
<mappers>
<mapper resource="com/xxx/domain/ProdcutMapper.xml"/>
</mappers> -->
<mappers>
<mapper resource="com/xiaoxin/bean/StudentMapper.xml" />
</mappers>
</configuration>
StudentMapper.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">
<!-- 定义访问数据表的SQL映射标签
命名空间相当于这份文件的一个id号
namespace : 命名空间(用来区分其它的mapper文件),一般为该文件所在的全路径包名+mapper文件名
一般在src下面的com开始
-->
<mapper namespace="com.xiaoxin.bean.StudentMapper">
<!-- 添加
id : 唯一的标识
parameterType : 参数类型(用来为sql语句中的占位符赋值) 可选
-->
<!-- <insert id="save" parameterType="com.xxx.mybatisqs.domain.Product">
insert into product(product_name, product_price) values(#{name}, #{price})
</insert> -->
<insert id="saveOne" parameterType="com.xiaoxin.bean.Student" >
insert into student(sname,age) values(#{sname},#{age})
</insert>
<select id="selectAll" resultType="com.xiaoxin.bean.Student" >
select * from student
</select>
<select id="selectOneById" parameterType="java.lang.Integer" resultType="com.xiaoxin.bean.Student" >
select * from student where sid = #{sid}
</select>
<update id="updateOneByid" parameterType="com.xiaoxin.bean.Student">
update student set sname = #{sname} where sid = #{sid}
</update>
<delete id="deleteOneByid" parameterType="java.lang.Integer" >
delete from student where sid = #{sid}
</delete>
</mapper>
3.更多参考
https://blog.csdn.net/zhongliwen1981/article/details/98586898