一 核心配置文件
二实体类
三数据库属性配置文件
四dao层sql语句映射
五测试类(以及工具类)
准备工作:配置config文件格式和mapper文件格式(复制到新建的xml文件第一行)或者进行myeclips设置自动生成
<?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 version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
导入jar包
UserMapper.java
package cn.kgc.dao;
import java.util.List;
import cn.kgc.pojo.User;
public interface UserMapper {
//查询用户的数据
public List<User> getUserList();
//count
public int count();
}
UserMapper.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.kgc.dao.UserMapper">
<!-- 查询记录数 -->
<!-- id:是唯一标识符 -->
<!-- resultType: 返回值类型 -->
<select id="count" resultType="int">
select count(*) as count from smbms_user
</select>
<select id="getUserList" resultType="User">
select * from smbms_user
</select>
</mapper>
User.java
package cn.kgc.pojo;
import java.util.Date;
public class User {
private Integer id;//
private String userCode;//用户编码
private String userName;//用户密码
private String userPassword;//密码
private Integer gender;
private Date birthday;
private String phone;
private String address;
private Integer userRole;
private Integer createdBy;
private Date creationDate;
private Integer modifyBy;
private Date modifyDate;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getUserRole() {
return userRole;
}
public void setUserRole(Integer userRole) {
this.userRole = userRole;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
}
MapperTest.java
package cn.kgc.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
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 org.junit.Test;
import cn.kgc.dao.UserMapper;
import cn.kgc.pojo.User;
import cn.kgc.utils.MyBatisUtil;
public class MapperTest {
//没有使用将公共的逻辑代码写入工具类,最初的方法
@Test
public void test() {
String resource="mybatis-config.xml";
int count=0;
SqlSession sqlSession=null;
try {
//1.获取mybatis-config.xml的输入流
InputStream is=Resources.getResourceAsStream(resource);
//2.建立SqlSessionFactory对象,来完成对配置文件的读取
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
//3.建立sqlSession对象
sqlSession=factory.openSession();
count=sqlSession.selectOne("cn.kgc.dao.UserMapper.count");
System.out.println(count);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
sqlSession.close();
}
}
//将公共的逻辑代码写入工具类,调用工具类中的静态方法。selectOne()方法获得数据条数
@Test
public void test2() {
int count=0;
SqlSession sqlSession=null;
try {
sqlSession=MyBatisUtil.createSqlSession();
count=sqlSession.selectOne("cn.kgc.dao.UserMapper.count");
System.out.println(count);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
MyBatisUtil.closeSqlSession(sqlSession);
}
}
//将公共的逻辑代码写入工具类,调用工具类中的静态方法。selectList()方法获得所有数据条信息
@Test
public void test3() {
List<User> userList=new ArrayList<User>();
SqlSession sqlSession=null;
try {
//1
sqlSession=MyBatisUtil.createSqlSession();
//2
userList=sqlSession.selectList("cn.kgc.dao.UserMapper.getUserList");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//3
MyBatisUtil.closeSqlSession(sqlSession);
}
for(User user :userList){
System.out.println(user.getUserCode()+"----"+user.getUserName());
}
}
//基于接口的方式
//将公共的逻辑代码写入工具类,调用工具类中的静态方法。调用接口中的抽象方法getUserList()获得所有数据条信息
@Test
public void test4() {
List<User> userList=new ArrayList<User>();
SqlSession sqlSession=null;
try {
sqlSession=MyBatisUtil.createSqlSession();
userList=sqlSession.getMapper(UserMapper.class).getUserList();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
MyBatisUtil.closeSqlSession(sqlSession);
}
for(User user :userList){
System.out.println(user.getUserCode()+"----"+user.getUserName());
}
}
}
MyBatisUtil.java
package cn.kgc.utils;
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 MyBatisUtil {
private static SqlSessionFactory factory;
//
static{
InputStream is;
try {
is = Resources.getResourceAsStream("mybatis-config.xml");
//2.建立SqlSessionFactory对象,来完成对配置文件的读取
factory=new SqlSessionFactoryBuilder().build(is);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static SqlSession createSqlSession(){
return factory.openSession(false);
}
public static void closeSqlSession(SqlSession sqlSession){
if(sqlSession!=null){
sqlSession.close();
}
}
}
database.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/smbms
user=root
password=root
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">
<!-- 通过这个配置文件完成mybatis与数据库的连接 -->
<configuration>
<!-- 引入database.properties 有下述两种方法-->
<!-- <properties resource="database.properties"></properties> -->
<properties>
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</properties>
<typeAliases>
<!-- <typeAlias type="cn.kgc.pojo.User" alias="User"/> -->
<package name="cn.kgc.pojo"/>
</typeAliases>
<environments default="aa">
<environment id="aa">
<!-- 配置事务管理 ,采用的是JDBC的事务管理-->
<transactionManager type="JDBC"></transactionManager>
<!--JDNI -->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="cn/kgc/dao/UserMapper.xml"/>
</mappers>
</configuration>