HTML+SSM + MySQL的简单登录(三)

三、配置MyBatis       

这篇是SSM简单整合的最后一步,mybatis整合,这些东西说难也难,你把步骤流程搞明白了,说简单也简单,有时候难易可能就是知道和不知道的区别。

        上篇位置:http://blog.csdn.net/u014360189/article/details/76347447

Spring SpringMVC改造的Demo添加MyBatis配置,完成最后的整合


1.添加MyBatis依赖包

可以在百度中搜索maven MyBatis

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.4</version>
</dependency>

添加mybatis-spring依赖包

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.1</version>
</dependency>

2.创建StudentMapper.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="Student">
	<resultMap type="com.lizem.check_right.bean.Student" id="StudentResultMap">
		<id column="id" jdbcType="INTEGER" property="id"/>
		<result  column="pwd" jdbcType="VARCHAR" property="pwd"/>
		<result  column="name" jdbcType="VARCHAR" property="name"/>
	</resultMap>
	
	<select id="selectStuById" parameterType="int" resultMap="StudentResultMap">
		select id, name, pwd from student where id = #{_parameter}
	</select>
</mapper>

3.spring-context.xml配置文件中配置数据源,sqlSessionFactorysqlSession

<!--1定义一个jdbc数据源,创建一个驱动管理数据源的bean -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations"
            value="classpath:com/lizem/check_right/sql/StudentMapper.xml"></property>
    </bean>
    
    <!-- 创建一个sqlSession对象 -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>
    
    <bean id="loginDAO" class="com.lizem.check_right.dao.LoginDAOImp">
		<property name="sqlSeesion" ref="sqlSession"></property>
	</bean>
4. 修改DAO 层,使用 sqlSeesion 来调用 mapper.xml 中配置的方法

public class LoginDAOImp implements ILoginDAO{
	SqlSession sqlSeesion;

	public SqlSession getSqlSeesion() {
		return sqlSeesion;
	}

	public void setSqlSeesion(SqlSession sqlSeesion) {
		this.sqlSeesion = sqlSeesion;
	}

	@Override
	public Student checkUserExist(int id, String pwd) {
		Student stu = null;
		
		//获取数据库中学生的信息
		Student stu_temp = sqlSeesion.selectOne("Student.selectStuById", id);
		
		//进行密码正误验证
		if(pwd.endsWith(stu_temp.getPwd())){
			stu = new Student();
			stu.setId(stu_temp.getId());
			stu.setName(stu_temp.getName());
		}	
		return stu;
	}
}

修改完毕,其他地方不用修改,mybatis主要是简化了链接数据库的工作,DAO层的获取数据和将数据pojo化现在都交由mybatis来做了。

使用三大框架时出现的一些莫名奇妙的问题特别多,不知道怎么搞搞又好了,所以遇到问题要善于总结问题,尤其是那种确定性的问题。

源代码位置:

链接:http://pan.baidu.com/s/1boUPMt5 密码:rftf


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值