模拟用户登录-SpringMVC+Spring+Mybatis整合小案例

1. 导入相关jar包

ant-1.9.6.jar
ant-launcher-1.9.6.jar
aopalliance.jar
asm-5.1.jar
asm-5.2.jar
aspectj-weaver.jar
cglib-3.2.4.jar
commons-logging-1.2.jar
javassist-3.21.0-GA.jar
log4j-1.2.17.jar
log4j-api-2.3.jar
log4j-core-2.3.jar
mybatis-3.4.2.jar
mybatis-spring-1.3.1.jar
mysql-connector-java-5.1.40-bin.jar
ognl-3.1.12.jar
slf4j-api-1.7.22.jar
slf4j-log4j12-1.7.22.jar
spring-aop-4.3.6.RELEASE.jar
spring-aspects-4.2.8.RELEASE.jar
spring-beans-4.3.6.RELEASE.jar
spring-context-4.3.6.RELEASE.jar
spring-context-support-4.3.6.RELEASE.jar
spring-core-4.3.6.RELEASE.jar
spring-expression-4.3.6.RELEASE.jar
spring-jdbc-4.2.8.RELEASE.jar
spring-orm-4.2.8.RELEASE.jar
spring-tx-4.2.8.RELEASE.jar
spring-web-4.3.6.RELEASE.jar
spring-webmvc-4.3.6.RELEASE.jar
taglibs-standard-compat-1.2.5.jar
taglibs-standard-impl-1.2.5.jar
taglibs-standard-jstlel-1.2.5.jar
taglibs-standard-spec-1.2.5.jar

2. mybatis.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>
  <mappers>
      <!-- 实体类映射文件 -->
      <mapper resource="com/bsss/entity/userloginMapper.xml"/>
  </mappers>
</configuration>

3. 实体类映射文件

<?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.bsss.entity.userloginMapper">

<resultMap type="com.bsss.entity.Userlogin" id="UserLoginResult">
    <id column="id" jdbcType="INTEGER" property="id"/>
    <result column="username" jdbcType="VARCHAR" property="username"/>
    <result column="userpassword" jdbcType="VARCHAR" property="userpassword"/>
    <result column="usertype" jdbcType="VARCHAR" property="usertype"/>
</resultMap>
  <select id="getUserlogin" parameterType="com.bsss.entity.Userlogin" resultMap="UserLoginResult">
    select id,username,userpassword,usertype from userlogin where 1 = 1
    <if test="username != null and !&quot;&quot;.equals(username)">and username=#{username}</if>
    <if test="userpassword != null and !&quot;&quot;.equals(userpassword)">and userpassword=#{userpassword}</if>
  </select>
</mapper>

4. bean.xml(spring)

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx.xsd
     http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

    <!-- 配置数据源 -->
     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
         <property name="url" value="jdbc:mysql://localhost:3306/bsss"></property>
         <property name="username" value="root"></property>
         <property name="password" value="root"></property>
     </bean>
     
     <!-- 配置sqlSessionFactory -->
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
         <property name="dataSource" ref="dataSource"></property>
         <property name="configLocation" value="classpath:com/bsss/conf/mybatis.xml"></property>
     </bean>
    
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
    </bean>
    
    <bean id="userLoginDAO" class="com.bsss.dao.impl.UserloginDAOImpl">
        <!-- property name必须与class中SqlSessionTemplate的变量名字保持一致 -->
        <property name="sqlSession" ref="sqlSessionTemplate"></property>
    </bean>
</beans>

5. 实体类

public class Userlogin {
    private int id;
    private String username;
    private String userpassword;
    private int usertype;
    
    public Userlogin() {
        super();
    }

    public Userlogin(int id, String username, String userpassword, int usertype) {
        super();
        this.id = id;
        this.username = username;
        this.userpassword = userpassword;
        this.usertype = usertype;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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 int getUsertype() {
        return usertype;
    }

    public void setUsertype(int usertype) {
        this.usertype = usertype;
    }
    
    @Override
    public String toString() {
        return "Userlogin [id=" + id + ", username=" + username + ", userpassword=" + userpassword + ", usertype="
                + usertype + "]";
    }
}

6. DAO接口及实现类

//接口
public interface UserloginDAO {
    public Userlogin getUserloginInfo(String username,String userpassword);
}

//实现类
public class UserloginDAOImpl implements UserloginDAO{
    private SqlSessionTemplate sqlSession;//变量名字必须与spring bean中的property name保持一致
    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    @Override
    public Userlogin getUserloginInfo(String username,String userpassword){
        Userlogin ul = new Userlogin();
        ul.setUsername(username);
        ul.setUserpassword(userpassword);
        ul = sqlSession.selectOne("com.bsss.entity.userloginMapper.getUserlogin", ul);
        return ul;
    }
}

7. 测试类

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext ac = new ClassPathXmlApplicationContext("com/bsss/conf/beans.xml");//spring-bean配置文件路径
        UserloginDAO uld = (UserloginDAO)ac.getBean("userLoginDAO");
        Userlogin ul = uld.getUserloginInfo("admin", "admin");
        System.out.println(ul);
    }

}

8. 执行结果

Userlogin [id=1, username=admin, userpassword=admin, usertype=0]

 

posted @ 2017-02-15 20:19  Realvie  阅读(1100)  评论(1编辑  收藏  举报