一、看一下项目结构吧
二、POM文件的一些配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>zhd.cn</groupId>
<artifactId>Mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Mybatis</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.3-1102-jdbc41</version>
</dependency>
</dependencies>
</project>
三、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="studentPojo" type="zhd.cn.Mybatis.pojo.StudentPojo" />
<!-- 使用自动扫描的形式来定义别名,在Role中使用@Alias("role")注解进行说明-->
<!-- <package name="com.jCuckoo.chapter02.pojo"/> -->
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/mydb" />
<property name="username" value="postgres" />
<property name="password" value="password" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="zhd/cn/Mybatis/mapper/StudentMapper.xml" />
</mappers>
</configuration>
四、Pojo和Mapper的配置
StudentPojo
package zhd.cn.Mybatis.pojo;
public class StudentPojo {
private Long id;
private String name;
private String sex;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
StudentMapper
package zhd.cn.Mybatis.mapper;
import zhd.cn.Mybatis.pojo.StudentPojo;
public interface StudentMapper {
public StudentPojo getStudentPojo(Long id);
public int insertStudentPojo(StudentPojo studentPojo);
public int deleteStudentPojo(Long id);
}
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">
<mapper namespace="zhd.cn.Mybatis.mapper.StudentMapper">
<select id="getStudentPojo" parameterType="long" resultType="studentPojo">
select * from student where id = #{id}
</select>
<insert id="insertStudentPojo" parameterType="studentPojo">
insert into student values(#{id},#{name},#{sex})
</insert>
<delete id="deleteStudentPojo" parameterType="long">
delete from student where id=#{id}
</delete>
</mapper>
五、会话工厂工具类
package zhd.cn.Mybatis.utils;
import java.io.IOException;
import java.io.InputStream;
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;
import org.apache.log4j.Priority;
public class SqlSessionFactoryUtils {
private static SqlSessionFactory sqlSessionFactory = null;
private static final Class CLASS_LOCK=SqlSessionFactoryUtils.class;
private SqlSessionFactoryUtils() {
}
public static SqlSessionFactory initSqlSessionFactory() {
String resource = "mybatis_config.xml";
InputStream inputStream = null;
try {
inputStream=Resources.getResourceAsStream(resource);
} catch (IOException e) {
Logger.getLogger(SqlSessionFactoryUtils.class.getName()).log(Priority.DEBUG, null, e);
}
synchronized (CLASS_LOCK) {
if(sqlSessionFactory==null) {
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
}
}
return sqlSessionFactory;
}
public static SqlSession openSqlSession() {
if(sqlSessionFactory==null) {
initSqlSessionFactory();
}
return sqlSessionFactory.openSession();
}
}
六、测试类
package zhd.cn.Mybatis;
import org.apache.ibatis.session.SqlSession;
import zhd.cn.Mybatis.mapper.StudentMapper;
import zhd.cn.Mybatis.pojo.StudentPojo;
import zhd.cn.Mybatis.utils.SqlSessionFactoryUtils;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
SqlSession sqlSession=null;
sqlSession=SqlSessionFactoryUtils.openSqlSession();
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
StudentPojo studentPojo =studentMapper.getStudentPojo((long) 1);
System.out.println(studentPojo.getId()+" "+studentPojo.getName());
studentPojo.setId((long) 2);
studentMapper.insertStudentPojo(studentPojo);
sqlSession.commit();
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。