MyBatics
是对持久化对象的操作自动转换为关系数据库的操作。
也就是在关系型数据库和对象之间作一个映射。
注:
对象持久化就是将对象存储在可持久保存的存储介质上,例如主流的关系数据库中。
<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/
cloud_study" />
<property name="username" value="root" />
<property name="password" value="123456" />
dataSource>
environment>
environments>
configuration>
例如:
创建一个user对象。
package com.micro.profession.mybatis;
public class User {
private int id;
private String userName;
private String corp; //corporation
public User(Integer id, String userName, String corp) {
this.id = id;
this.userName = userName;
this.corp = corp;
}
public User(String userName, String corp) {
this.userName = userName;
this.corp = corp;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getCorp() {
return corp;
}
public void setCorp(String corp) {
this.corp = corp;
}
}
Mybatis 是用对象的操作与关系型数据库的sql语句进行映射的。
例如:
package com.micro.profession.mybatis;
public interface GetUserInfo {
public User getUser(int id);
}
<mapper namespace="com.micro.profession.mybatis.GetUserInfo">
<select id="getUser" parameterType="int"
resultType="com.micro.profession.mybatis.User">
select id ,userName,corp from user where id =#{id}
select>
mapper>
编写mapper文件完成之后,需要添加在主配置SqlSessionFactory中:在mappers中添加mapper。
<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/
cloud_study" />
<property name="username" value="root" />
<property name="password" value="123456" />
dataSource>
environment>
environments>
<mappers>
<mapper resource="com/micro/profession/mybatis/userMapper.xml" />
mappers>
configuration>
package com.micro.profession.mybatis;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class HelloMyBatis {
public static void main(String[] args) {
//1. 声明配置⽂件的目录
String resource = "conf.xml";
//2. 加载应⽤配置⽂件
InputStream is = HelloMyBatis.class.getClassLoader()
.getResourceAsStream(resource);
// 3. 创建SqlSessonFactory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder()
.build(is);
// 4. 获取Session
SqlSession session = sessionFactory.openSession();
try {
// 5. 获取操作类
GetUserInfo getUserInfo = session.getMapper(GetUserInfo.class);
// 6. 完成查询操作
User user = getUserInfo.getUser(11);
System.out.println(user.getId() + " " + user.getUserName() + " "+ user.getCorp());
} finally {
// 7.关闭Session
session.close();
}
}
}
注意:
接口类的定义:
package com.micro.profession.mybatis;
public interface UserOp {
public void addUser(User user);
public void updateUser(User user);
public void deleteUser(int id);
public User getUser(int id);
}
mapper配置
<mapper namespace="com.micro.profession.mybatis.UserOp">
<insert id="addUser" parameterType="com.micro.profession.mybatis.User"
useGeneratedKeys="true" keyProperty="id">
insert into user (userName,corp)
values(#{userName},#{corp})
insert> <select id="getUser" parameterType="int"
resultType="com.micro.profession.mybatis.User">
select id ,userName,corp from user where id =#{id}
select> <update id="updateUser" parameterType="com.micro.profession.mybatis.User">
update user set userName =
#{userName} , corp = #{corp}
where id = #{id}
update> <delete id="deleteUser" parameterType="com.micro.profession.mybatis.User">
delete from user
where id =
#{id}
delete>
mapper>
package com.micro.profession.mybatis;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class HelloMyBatisMoreOp {
public static void main(String[] args) {
moreOp();
}
public static void moreOp() {
// 1. 声明配置⽂件
String resource = "conf.xml";
// 2. 加载应⽤配置⽂件
InputStream is = HelloMyBatisMoreOp.class.getClassLoader()
.getResourceAsStream(resource);
// 3. 创建SqlSessonFactory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder()
.build(is);
//mybatis是自动以事务提交的,我们需要显示的调用, 我们可用下面这句话,将事务提交给关闭掉。
SqlSession session = sessionFactory.openSession(true);
try {
// 5. 获取操作类
UserOp userOp = session.getMapper(UserOp.class);
User user = new User("XiaoMing", "Netease");
// 插⼊⽤户
userOp.addUser(user);
System.out.println(user.getId());
// 查询⽤户
user = userOp.getUser(user.getId());
System.out.println("userId:" + user.getId() + ", userName:"
+ user.getUserName() + ", corp:" +
user.getCorp());
user.setUserName("LiMing");
// 更新⽤户
userOp.updateUser(user);
// 删除⽤户
userOp.deleteUser(user.getId());
} finally {
// 7.关闭Session
session.close();
} } }
在操作对象的操作方法上编写注解:
例如:
package com.micro.profession.mybatis;
public interface GetUserInfo {
@Select("select * from user where id = #{id} ")
public User getUser(int id);
}
这样就不需要在编写mapper文件了。
但是我们需要在代码中指定映射关系。
需要在(3. 创建SqlSessonFactory)添加下面这两句:
configuration conf = sessionFactory.getConfiguration();
//获取配置
conf.addMapper(GetUserInfo.class);
//添加对应的类的CLASS对象
package com.micro.profession.mybatis;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class HelloMyBatis {
public static void main(String[] args) {
//1. 声明配置⽂件的目录
String resource = "conf.xml";
//2. 加载应⽤配置⽂件
InputStream is = HelloMyBatis.class.getClassLoader()
.getResourceAsStream(resource);
// 3. 创建SqlSessonFactory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder()
.build(is);
configuration conf = sessionFactory.getConfiguration();
conf.addMapper(GetUserInfo.class);
// 4. 获取Session
SqlSession session = sessionFactory.openSession();
try {
// 5. 获取操作类
GetUserInfo getUserInfo = session.getMapper(GetUserInfo.class);
// 6. 完成查询操作
User user = getUserInfo.getUser(11);
System.out.println(user.getId() + " " + user.getUserName() + " "+ user.getCorp());
} finally {
// 7.关闭Session
session.close();
}
}
}