一、下载mybatis和数据库驱动的jar,我这里用到的数据库是pgAdmin III ,所以我下载的jar包分别为mybatis-3.0.2 jar和po's'tgresql-42.2.1.jar,如图所示

eclipse 整合mybatis的过程-LMLPHP

二、创建一个java项目,导入下载的jar包,构建的结构如下图所示:

eclipse 整合mybatis的过程-LMLPHP

三、分析

1.现在数据库中创建一个名为mybatis的数据库:创建的列有id(自动生长的)、name、sex、age,具体的创建过程自己创建。

2.先创建一个User.java类,如下图:

package com.mybatis.model.impl;

public class User {
private Integer id;
private String name;
private String sex;
private Integer age;
public User()
{
}
public User(final String name,final String sex,final Integer age)
{
this.name=name;
this.sex=sex;
this.age=age;
}
public Integer getId()
{
return this.id;
}
public void setId(final Integer id)
{
this.id=id;
} public String getName()
{
return this.name;
}
public void setName(final String name)
{
this.name=name;
} public String getSex()
{
return this.sex;
}
public void setSex(final String sex)
{
this.sex=sex;
} public Integer getAge()
{
return this.age;
}
public void setAge(final Integer age)
{
this.age=age;
}
@Override
public String toString()
{
return "User [id="+id+",name="+name+",sex="+sex+",age"+age+"]";
}
}

3.创建一个UserMapper.java接口,这里只是负责提供数据CRUD的接口,具体的实现在下面的UserMapper.xml文件中。

package com.mybatis.mapper;

import java.util.List;

import com.mybatis.model.impl.User;

public interface UserMapper {

    public void insertUser(User user) throws Exception;

    public User selectOneUser(int id) throws Exception;

    public List<User> selectAllUser() throws Exception;

    public void deleteUser() throws Exception;

}

4.创建一个xml文件,名为UserMapper.xml,映射到上面的UserMapper接口。

<?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="com.mybatis.mapper.UserMapper"><!-- 这个是映射到上面的UserMapper接口的 -->
<insert id="insertUser" parameterType="com.mybatis.model.impl.User" useGeneratedKeys="true"
keyProperty="id" >
insert into tb_user(name,sex,age)
values(#{name},#{sex},#{age})
</insert> <select id="selectAllUser" resultType="com.mybatis.model.impl.User">
select * from tb_user
</select>
<delete id="deleteUser">
delete from tb_user </delete>
<select id="selectOneUser" parameterType="java.lang.Integer"
resultType="com.mybatis.model.impl.User">
select * from tb_user where id=#{id}
</select>
</mapper>

5.创建一个mybatis.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"> <!-- XML配置文件包含对MyBatis系统的核心设置 -->
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<!-- <property name="driver" value="com.mysql.jdbc.Driver" /> -->
<property name="driver" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/mybatis"/>
<property name="username" value="postgres" />
<property name="password" value="p" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>

6.创建一个MybatisTest.java类类运行测试。

package com.mybatis.test;
import java.io.Reader;
import java.util.List; 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 com.mybatis.mapper.UserMapper;
import com.mybatis.model.impl.User;; public class MyBatisTest { public static void main(final String[] args) throws Exception{
// TODO Auto-generated method stub
//读取MyBatis配置文件
final Reader reader=Resources.getResourceAsReader("mybatis.xml");;
//初始化mybatis,创建SqlSessionFactory类的实例。
final SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
//创建Session实例
final SqlSession session=sqlSessionFactory.openSession();
//创建User对象
final UserMapper userMapper=session.getMapper(UserMapper.class);
//插入数据
User user=new User("Carson","男",23);
userMapper.insertUser(user);
user=new User("THL","女",23);
userMapper.insertUser(user);
session.commit(); //查询数据
final List<User> list=userMapper.selectAllUser();
System.out.println(list); //删除数据
// userMapper.deleteUser();
session.commit();
session.close(); } }

6.运行的结果如图,这是在没有运行删除语句时产生的结果。

eclipse 整合mybatis的过程-LMLPHP

以上是个人代码,如果有要转发的,请点赞,thx~.

05-29 00:34