SSM框架整合_1MyBatis搭建

北城余情 提交于 2019-11-27 09:53:53

环境描述:MyBatis3.2.2、mysql5、Spring3.2、SpringMvc3.2

在整合ssm框架之前,先让mybatis跑起来;

  1. 导入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());
	}
	
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!