0基础搭建mybatis环境
本文是自己搭建mybatis环境的过程,没有写太多的太深的原理,在这里只是记载一下搭建的基本过程,同时供想要接触mybatis的同仁们一个参考。当你把这个文档读完后,并在自己的机器上运行成功时,你已经对mybatis有了一个基本的了解与掌握了简单的CRUD数据库交互操作。
(不足之处,请指正)
1基础准备
1)开发环境jdk1.6(1.6_20),IDE工具:eclipse-galileo版,数据库:sql2008(r1版)
2)资料准备:首先本文中mybatis.jar是mybatis-3.2.2.jar(可以百度下载,也可以从本文的链接中下载) ,jdbc驱动:sqljdbc.jar(因为数据库是sql的,所以要用这个jar包)
2项目架构
1)在eclipse中创建java工程:ibatisTest(如图)
2)在ibatisTest项目中添加lib文件夹,并把需要用的资源文件(mybatis-3.2.2.jar、sqljdbc.jar)放在lib中。
在项目ibatisTest上右键-properties-java build path-libraries中添加lib下面的mybatis-3.2.2.jar、sqljdbc.jar文件(如图)。
3mybatis 配置xml
1)在config文件夹中添加xml文件:Configuration.xml(建议直接copy到你的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 type="MybatisTest.pojo.Acount" alias="Acount" />
</typeAliases>
<!-- 环境节点,配置数据连接相关的信息 -->
<environments default="development">
<environment id="development">
<transactionManager type="jdbc"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="url" value="jdbc:sqlserver://127.0.0.1:1433;databaseName=gdss3.0"/>
<property name="username" value="grainer"/>
<property name="password" value="password"/>
</dataSource>
</environment>
</environments>
<!-- 配置SQL映射语句 -->
<mappers>
<mapper resource="MybatisTest/dao/AcountMapper.xml" />
</mappers>
</configuration>
4创建Session链接工具
在util文件夹中添加java代码:SqlSessionHelper.java(这个文件是通过mybatis进行数据库连接的)
package MybatisTest.util;
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;
public class SqlSessionHelper {
private static final String CONFIG_PATH = "config/Configuration.xml";
/* 打开session */
public static SqlSession getSqlSession(){
SqlSession session = null;
try{
InputStream stream = Resources.getResourceAsStream(CONFIG_PATH);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(stream);
session = factory.openSession();
}catch(Exception e){
e.printStackTrace();
}
return session;
}
/* 关闭 session*/
public static void closeSession(SqlSession session){
session.close();
}
}
5创建POJO对象与数据库表acount结构
package MybatisTest.pojo;
import java.io.Serializable;
/**
* This is an object that contains data related to the Acount table.
* Do not modify this class because it will be overwritten if the configuration file
* related to this class is modified.
*
* @mybatis.class
* table="Acount"
*/
public class Acount implements Serializable{
private static final long serialVersionUID = 1L;
private java.lang.Integer acount_id;
private java.lang.String acount_name;
private java.lang.String acount_login_name;
private java.lang.String acount_login_password;
private boolean acount_online;
private java.lang.Integer acount_identity_type;
private java.lang.Integer acount_status;
private java.lang.Integer organization_employee;
private int hashCode = Integer.MIN_VALUE;
public java.lang.Integer getAcount_id() {
return acount_id;
}
public void setAcount_id(java.lang.Integer acountId) {
acount_id = acountId;
}
public java.lang.String getAcount_name() {
return acount_name;
}
public void setAcount_name(java.lang.String acountName) {
acount_name = acountName;
}
public java.lang.String getAcount_login_name() {
return acount_login_name;
}
public void setAcount_login_name(java.lang.String acountLoginName) {
acount_login_name = acountLoginName;
}
public java.lang.String getAcount_login_password() {
return acount_login_password;
}
public void setAcount_login_password(java.lang.String acountLoginPassword) {
acount_login_password = acountLoginPassword;
}
public boolean isAcount_online() {
return acount_online;
}
public void setAcount_online(boolean acountOnline) {
acount_online = acountOnline;
}
public java.lang.Integer getAcount_identity_type() {
return acount_identity_type;
}
public void setAcount_identity_type(java.lang.Integer acountIdentityType) {
acount_identity_type = acountIdentityType;
}
public java.lang.Integer getAcount_status() {
return acount_status;
}
public void setAcount_status(java.lang.Integer acountStatus) {
acount_status = acountStatus;
}
public java.lang.Integer getOrganization_employee() {
return organization_employee;
}
public void setOrganization_employee(java.lang.Integer organizationEmployee) {
organization_employee = organizationEmployee;
}
}
CREATE TABLE [Acount](
[acount_id] [int] IDENTITY(1,1) NOT NULL,
[acount_name] [varchar](100) NULL,
[acount_login_name] [varchar](200) NULL,
[acount_login_password] [varchar](200) NULL,
[acount_online] [bit] NULL,
[acount_identity_type] [int] NOT NULL,
[acount_status] [int] NULL,
[organization_employee] [int] NULL,
CONSTRAINT [PK1] PRIMARY KEY CLUSTERED
(
[acount_id] ASC
)
6配置mapperxml:配置映射文件是重点(此处用到接口的方式编程)
1)新建一个映射文件xml(AcountMapper.xml) AcountMapper.xml中是我们的写sql脚本(增删改查)
<?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="MybatisTest.dao.AcountMapper">
<!-- id="selectAcountById" 一定要与AcountMapper.java文件中方法名称一致 -->
<select id="selectAcountById" parameterType="int" resultType="Acount">
select * from Acount where acount_id = #{id}
</select>
<resultMap type="Acount" id="resultAcountLists">
</resultMap>
<select id="selectAcountLists" resultMap="resultAcountLists">
select * from Acount
</select>
<insert id="addAcount" parameterType="Acount"
useGeneratedKeys="true" keyProperty="acount_id">
insert into Acount(acount_name,acount_login_name,acount_login_password,acount_online,acount_identity_type,acount_status,organization_employee)
values(#{acount_name},#{acount_login_name},#{acount_login_password},#{acount_online},#{acount_identity_type},#{acount_status},#{organization_employee})
</insert>
<update id="updateAcount" parameterType="Acount" >
update Acount set acount_name=#{acount_name},acount_login_name=#{acount_login_name},acount_login_password=#{acount_login_password} where acount_id=#{acount_id}
</update>
<delete id="deleteAcount" parameterType="int">
delete from Acount where acount_id=#{id}
</delete>
</mapper>
2)新建java文件(AcountMapper.java) AcountMapper.java是与AcountMapper.xml对应的接口文件
package MybatisTest.dao;
import java.util.List;
import MybatisTest.pojo.Acount;
public interface AcountMapper {
public Acount selectAcountById(int id);
public List<Acount> selectAcountLists();
public void addAcount(Acount acount);
public void updateAcount(Acount acount);
public void deleteAcount(int id);
}
以上是整个环境的搭建过程。
1测试mybatis环境
新建test.java文件用来进行测试
package MybatisTest;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import MybatisTest.dao.AcountMapper;
import MybatisTest.pojo.Acount;
import MybatisTest.util.SqlSessionHelper;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
SqlSession session = SqlSessionHelper.getSqlSession();
AcountMapper actionMapper = session.getMapper(AcountMapper.class);
Acount newACT = new Acount();//C操作
newACT.setAcount_name("lisi");
newACT.setAcount_login_name("hanlei");
newACT.setAcount_identity_type(2);
newACT.setAcount_login_password("andyou");
newACT.setAcount_online(true);
newACT.setAcount_status(1);
newACT.setOrganization_employee(2);
actionMapper.addAcount(newACT);
actionMapper.deleteAcount(9);//D操作
Acount act = actionMapper.selectAcountById(10);//R操作
act.setAcount_login_password("passw0rd");
actionMapper.updateAcount(act);//U操作
//session.commit(); 本例是测试,commit提交事件注释掉
List<Acount> actList = actionMapper.selectAcountLists();
for (Acount acount : actList) {
System.out.println(acount.getAcount_name()+" : "+acount.getAcount_login_password());
}
}
}
运行结果:
2附件
本文的源码文件下载地址: (包含mybatis-3.2.2.jar资源文件)