一.配置环境
1.首先我们要导入相应的包:
下载地址:
https://github.com/mybatis/mybatis-3/releases
2.然后我们开始要配置数据库连接环境
在src文件下创建一个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">
<configuration>
<typeAliases>
<typeAlias alias="role" type="mybaits.pojo.Role"/>//关联pojo对象
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="autoCommit" value="false"/>
</transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/jdbc?&useSSL=false&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mybaits\mapper\RoleMapper.xml"/>//关联映射文件
</mappers>
</configuration>
获取连接的类:
package mybaits.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
public class SqlSessionFactoryUtil {
private static SqlSessionFactory sqlSessionFactory=null;
private static final Class CLASS_LOCK=SqlSessionFactoryUtil.class;
private SqlSessionFactoryUtil() {}
public static SqlSessionFactory initSqlSessionFactory() {
String resource="mybaits-config.xml";
InputStream inputStream=null;
try {
inputStream = Resources.getResourceAsStream(resource);
}catch(IOException ex) {
Logger.getLogger(SqlSessionFactoryUtil.class.getName()).log(null,ex);
}
synchronized(CLASS_LOCK) {
if(sqlSessionFactory==null) {
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
}
}
return sqlSessionFactory;
}
public static SqlSession openSqlSession() {
if(sqlSessionFactory==null) {
initSqlSessionFactory();
}
return sqlSessionFactory.openSession();
}
}
3.接着我们要编写对应的pojo对象和映射文件和操作接口
pojo
package mybaits.pojo;
public class Role {
private Long id;
private String roleName;
private String note;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
}
mysql创建表的语句:
mysql> create table t_role(
-> id int,
-> role_name varchar(40),
-> note varchar(50));
Query OK, 0 rows affected (0.25 sec)
映射文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mybaits.mapper.RoleMapper">
<select id="getRole" parameterType="long" resultType="role">
select id,role_name as roleName,note from t_role where id = #{id}
</select>
<insert id="insertRole" parameterType="role">
insert into t_role(role_name,note) values (#{roleName},#{note})
</insert>
<delete id="deleteRole" parameterType="long">
delete from t_role where id = #{id}
</delete>
</mapper>
操作接口:
package mybaits.mapper;
import mybaits.pojo.Role;
public interface RoleMapper {
public Role getRole(Long id);
public int deleteRole(Long id);
public int insertRole(Role role);
}
最后,编写主类测试:
package mybaits.main;
import java.io.IOException;
import org.apache.ibatis.session.SqlSession;
import mybaits.mapper.RoleMapper;
import mybaits.pojo.Role;
import mybaits.util.SqlSessionFactoryUtil;
public class Main {
public static void main(String[] args) throws IOException {
SqlSession sqlSession = null;
try {
sqlSession = SqlSessionFactoryUtil.openSqlSession();
RoleMapper roleMapper=sqlSession.getMapper(RoleMapper.class);
Role role = new Role();
role.setRoleName("testName");
role.setNote("testNote");
roleMapper.insertRole(role);
sqlSession.commit();
}catch(Exception ex) {
System.err.println(ex.getMessage());
sqlSession.rollback();
}finally {
if(sqlSession!=null) {
sqlSession.close();
}
}
}
}
测试结果: