这里是一个简单的MyBatis实例,实例的主要内容是对角色表的增删改查。
log4j.properties
log4j.rootLogger=DEBUG , stdout
log4j.logger.org.mybatis=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n
我们把它设置为DEBUG级别,让它能够把详细的日志打印处理,一边调试。
Role.java
package 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;
}
}
RoleMapper.java
package mapper;
import pojo.Role;
import java.util.List;
public interface RoleMapper {
public int insertRole(Role role);
public int deleteRole(Long id);
public int updateRole(Role role);
public Role getRole(Long id);
public List<Role> findRoles(String roleName);
}
RoleMapper.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">
<mapper namespace="mapper.RoleMapper">
<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>
<update id="updateRole" parameterType="role">
update t_role set role_name=#{roleName},note=#{note} where id=#{id}
</update>
<select id="findRoles" parameterType="string" resultType="role">
select id,role_name as roleName,note from t_role where role_name like concat('%',#{roleName},'%')
</select>
<select id="getRole" parameterType="long" resultType="role">
select id,role_name as roleName,note from t_role where id=#{id}
</select>
</mapper>
mybatis-config.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="pojo.Role"></typeAlias>
</typeAliases>
<!-- 数据库环境 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/ssm"></property>
<property name="username" value="root"></property>
<property name="password" value="hjw19990825"></property>
</dataSource>
</environment>
</environments>
<!-- 映射文件 -->
<mappers>
<mapper resource="mapper/RoleMapper.xml"></mapper>
</mappers>
</configuration>
SqlSessionFactoryUtils.java
package utils;
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 java.io.IOException;
import java.io.InputStream;
public class SqlSessionFactoryUtils {
private final static Class<SqlSessionFactoryUtils> LOCK= SqlSessionFactoryUtils.class;
private static SqlSessionFactory sqlSessionFactory=null;
//构造方法
private SqlSessionFactoryUtils(){}
public static SqlSessionFactory getSqlSessionFactory(){
synchronized (LOCK){
if(sqlSessionFactory!=null){
return sqlSessionFactory;
}
String resource="mybatis-config.xml";
InputStream inputStream;
try {
inputStream= Resources.getResourceAsStream(resource);
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
return null;
}
return sqlSessionFactory;
}
}//getSqlSessionFactory()
public static SqlSession openSqlSession(){
if(sqlSessionFactory==null){
getSqlSessionFactory();
}
return sqlSessionFactory.openSession();
}
}
Test.java
import mapper.RoleMapper;
import org.apache.ibatis.session.SqlSession;
import pojo.Role;
import utils.SqlSessionFactoryUtils;
import java.util.logging.Logger;
public class Test {
public static void main(String[] args){
Logger log= Logger.getLogger(String.valueOf(Test.class)); //日志
SqlSession sqlSession=null;
try{
sqlSession= SqlSessionFactoryUtils.openSqlSession();
RoleMapper roleMapper=sqlSession.getMapper(RoleMapper.class);
Role role=roleMapper.getRole(1L);
log.info(role.getRoleName());
}catch (Exception ex){
ex.printStackTrace();
}finally {
if(sqlSession!=null){
sqlSession.close();
}
}
}
}