環境描述:MyBatis3.2.2、mysql5、Spring3.2、SpringMvc3.2
在整合ssm框架之前,先讓mybatis跑起來;
- 導入MyBatis框架所需的Jar 包
2. Mybatis 配置文件my-batis.xml
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/smbms_db
userName=root
pwd=root
<?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" />
<settings>
<!-- 配置mybatis的log實現爲LOG4J -->
<setting name="logImpl" value="LOG4J" />
</settings>
<!-- 別名設置 -->
<typeAliases>
<package name="com.liuh.smbms.pojo" />
</typeAliases>
<environments default="development">
<environment id="development">
<!--配置事務管理,採用JDBC的事務管理 -->
<transactionManager type="JDBC"></transactionManager>
<!-- POOLED:mybatis自帶的數據源,JNDI:基於tomcat的數據源 -->
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${userName}" />
<property name="password" value="${pwd}" />
</dataSource>
</environment>
</environments>
<!-- 將mapper文件加入到配置文件中 -->
<mappers>
<package name="com/liuh/smbms/dao" />
</mappers>
</configuration>
3. 編寫Dao層
package com.liuh.smbms.dao;
import java.util.List;
import com.liuh.smbms.pojo.User;
public interface UserMapper {
/**
* 通過userCode獲取User
*
* @param connection
* @param userCode
* @return @
*/
public User getLoginUser(String userCode);
}
編寫Dao 實現
<?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.liuh.smbms.dao.UserMapper">
<select id="getLoginUser" resultType="user">
select * from smbms_user where userCode=#{userCode}
</select>
</mapper>
4.編寫實體Bean
package com.liuh.smbms.pojo;
import java.util.Date;
public class User {
private Integer id; //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; //更新時間
private Integer age;//年齡
private String userRoleName; //用戶角色名稱
public User(){}
public User(Integer id,String userCode,String userName,String userPassword,Integer gender,Date birthday,String phone,
String address,Integer userRole,Integer createdBy,Date creationDate,Integer modifyBy,Date modifyDate){
this.id = id;
this.userCode = userCode;
this.userName = userName;
this.userPassword = userPassword;
this.gender = gender;
this.birthday = birthday;
this.phone = phone;
this.address = address;
this.userRole = userRole;
this.createdBy = createdBy;
this.creationDate = creationDate;
this.modifyBy = modifyBy;
this.modifyDate = modifyDate;
}
public String getUserRoleName() {
return userRoleName;
}
public void setUserRoleName(String userRoleName) {
this.userRoleName = userRoleName;
}
public Integer getAge() {
/*long time = System.currentTimeMillis()-birthday.getTime();
Integer age = Long.valueOf(time/365/24/60/60/1000).IntegerValue();*/
Date date = new Date();
Integer age = date.getYear()-birthday.getYear();
return age;
}
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;
}
}
5. 編寫測試類,工具類
package com.liuh.smbms.test;
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;
/**
* @note myBatis 工具類
* @author liuh
* */
public class BatisUtil {
static SqlSessionFactory ssf = null;
static {
// 讀取mybatis配置文件
try {
InputStream is = Resources.getResourceAsStream("my-batis.xml");
ssf = new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @note 創建sqlSession
* @author liuh
* */
public static SqlSession creatSqlSession() {
return ssf.openSession();
}
/**
* @note 關閉sqlSeesion
* @author liuh
* */
public static void closeSqlSession(SqlSession session) {
if (null != session) {
session.close();
}
}
}
package com.liuh.smbms.test;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.Test;
import com.liuh.smbms.dao.UserMapper;
import com.liuh.smbms.pojo.User;
public class UserTest {
private Logger logger = Logger.getLogger(UserTest.class);
@Test
public void testLogin() {
SqlSession sqlSession= BatisUtil.creatSqlSession();
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
User user= userMapper.getLoginUser("admin");
System.out.println(user.getUserPassword());
}
}