MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除 了几乎所有的 JDBC 代码和参数的手工设置以及结果集的检索。
mybatis_官方资源下载地址:https://code.google.com/p/mybatis/downloads/list?can=1&q=&colspec=Filename+Product+Type+Summary+Uploaded+Size+DownloadCount
所需要的jar
mybatis-3.2.3.jar
mysql-connector-java-5.1.28-bin.jar
1、配置文件mybatis-config.xml
2、根据数据库创建响应的实体类
3、创建mapper文件完成对实体类的映射
4、创建sqlsession,并且通过sqlsession完成对数据库的操作
1、配置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>
<!-- 数据库配置文件 -->
<properties resource="jdbc.properties" />
<!-- 类型别名 -->
<typeAliases>
<typeAlias type="cn.test.model.User" alias="User"/>
</typeAliases>
<!-- 环境 开发环境,发布环境 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!-- 将map文件加入到配置文件中 -->
<mappers>
<mapper resource="cn/test/model/User.xml"/>
</mappers>
</configuration>
jdbc.properties
username=root
password=root
url=jdbc:mysql://localhost:3306/mybatis_db
driver=com.mysql.jdbc.Driver
2、创建实体类
cn.test.model.User
package cn.test.model;
public class User {
int id ;
String name ;
int clsid ;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getClsid() {
return clsid;
}
public void setClsid(int clsid) {
this.clsid = clsid;
}
}
3、创建mapper文件对应的实体类映射
cn/test/model/User.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="cn.test.mapper.UserMapper">
<insert id="add" parameterType="User">
insert into stu (name,clsid)
value (#{name},#{clsid})
</insert>
<update id="update" parameterType="User">
update stu set name=#{name} ,clsid=#{clsid}
where id = #{id}
</update>
<delete id="delete" parameterType="int">
delete from stu where id = #{id}
</delete>
<select id="select" parameterType="int" resultType="User">
select * from stu where id = #{id}
</select>
<select id="list" resultType="User">
select * from stu
</select>
<select id="find" resultType="User" parameterType="map" >
select * from stu
<if test="name!=null">where (name like #{name})</if>
order by ${id} ${sort} limit #{pageOffset},#{pageSize}
</select>
</mapper>
4、创建sqlsession,并且通过sqlsession完成对数的操作
UtilTestMybatis.java
public class UtilTestMybatis {
public static void main(String[] args) {
//1、创建配置文件
InputStream is;
try {
is = Resources.getResourceAsStream("mybits-conf.xml");
//2、创建sqlsessionFatory
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
//3、创建sqlsession
SqlSession sqlSession = factory.openSession();
//4、调用mapper文件插入数据
User user = new User();
user.setClsid(1);
user.setName("sutName1");
sqlSession.insert("cn.test.model.User.add",user);
sqlSession.commit();
sqlSession.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}