基于社区医疗系统的spring+mybatis架构设计流程----登录

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_34233802/article/details/53149002

本文完整介绍了的 登录 的前后台工作过程。对于其他前后台关于数据库的增删改查操作适用于同样的流程。
1 在web.xml中加入数据库的相应配置文件

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

上面代码的用于加载容器初始化时所需配置文件和相应路径,便于将配置文件不直接放在web.xml中。当为加classpath时,自动在web-inf 路径下寻找。
2 配置数据库连接

<context:property-placeholder location="classpath:jdbc.properties"/>  
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${driverClassName}" />
        <property name="url" value="${url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    <!-- 配置工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis.cfg.xml"/>
    </bean>

在jdbc.properties中写入连接数据库的用户名密码

driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1433; DatabaseName=ST
jdbc.username=sa
jdbc.password=123456

3 在对应的mybatis.cfg.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> 
    <typeAliases><!-- 为实体类定义别名 -->
        <package name="com.communityhealth.model"/>
    </typeAliases>
    <mappers>
            <mapper resource="com/communityhealth/mapper/cardCheckMapper.xml"/>

</configuration>

mapper中配置sql语句

<?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.communityhealth.mapper.cardCheckMapper">
    <resultMap type="com.communityhealth.model.Logincheck" id="checklogin">
        <result property="gh" column="GH"/>
        <result property="xm" column="XM"/>
        <result property="xb" column="XB"/>
        <result property="ssksdm" column="SSKSDM"/>
        <result property="pass" column="PASS"/>
    </resultMap>
    <select id="Logincheck" parameterType="Map" resultMap="checklogin">
        select * from dbo.TB_DIC_CZYXX where XM=#{name} and PASS=#{password}
    </select>
</mapper>

4 依次配置spring的model层、dao层、Service层、Controller层。

//model层
public class CardCheck {
    private String klx;
    private String kh;
    //省略部分。。。
        public String getKlx() {
        return klx;
    }
    public void setKlx(String klx) {
        this.klx = klx;
    }
    public String getKh() {
        return kh;
    }
    public void setKh(String kh) {
        this.kh = kh;

//dao层用于连接数据库交互
package com.communityhealth.dao.impl;


@Repository//标注数据访问组件
public class LoginCheckDaoImpl extends SqlSessionDaoSupport implements LoginCheckDao{
    @Resource
    @Override
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
        super.setSqlSessionFactory(sqlSessionFactory);
    }
    //@Override
    public Logincheck getLogincheck(String name,String password) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("name",name);
        map.put("password", password);
        String statement = "com.communityhealth.mapper.treatementMapper.Logincheck";
        Logincheck pass = this.getSqlSession().selectOne(statement, map);

        return pass;
    }

}
//service层
package com.communityhealth.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.communityhealth.dao.CardCheckDao;
import com.communityhealth.model.CardCheck;
import com.communityhealth.service.CardCheckService;
@Service//标识服务层
public class CardCheckServiceImpl implements CardCheckService{
    private CardCheckDao cardCheckDao;
    public CardCheck getCardCheck(String number) {

        return cardCheckDao.getCardCheck(number);
    }
    public CardCheckDao getCardCheckDao() {
        return cardCheckDao;
    }
    @Resource
    public void setCardCheckDao(CardCheckDao cardCheckDao) {
        this.cardCheckDao = cardCheckDao;
    }

}
//Controller层
package com.communityhealth.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.junit.Test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.communityhealth.dao.LoginCheckDao;
import com.communityhealth.dao.impl.LoginCheckDaoImpl;
import com.communityhealth.model.Logincheck;
import com.communityhealth.service.LoginService;
import com.communityhealth.service.impl.LoginServiceImpl;

/**
 * @author cc
 *
 */
@Controller
public class LoginController {
    @Resource
    private LoginService loginService;
    @RequestMapping("/login")
    public ModelAndView login(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{

        //System.out.println("logincheck");
        ModelAndView model = new ModelAndView();
        model.setViewName("Overview");//web-info/jsp  hello .jsp
        ModelAndView model1 = new ModelAndView();
        model1.setViewName("a");
        String name = request.getParameter("name");
        String password = request.getParameter("password");

        Logincheck check = loginService.getLogincheck(name, password);  
        //根据是否查询到结果,跳转对应页面
        if (check !=null) {   
            request.getSession(true).setAttribute("name", name);
            return model;
        }
        else {
            return model1;
        }
    }

前台页面:

<form role="form" action="login" method="post" class="form-horizontal">
                            <div class="form-group">
                                <label for="username" class="col-sm-3 control-label">用户名:</label>
                                <div class="col-sm-9">
                                  <input type="text" class="form-control" name="name" id="username" placeholder="用户名">
                                </div>
                              </div>
                            <div class="form-group">
                                <label for="password" class="col-sm-3 control-label">密码:</label>
                                <div class="col-sm-9">
                                  <input type="password" class="form-control" name="password" id="password" placeholder="密码">
                                </div>
                            </div>
                            <button type="submit" class="btn btn-info btn-lg btn-block">登录</button>
                        </form>

一个model层对应一张表,一个dao层可对应多个model。多个model放在一个dao里面可以使代码结构简洁,但也会违反java开闭原则

猜你喜欢

转载自blog.csdn.net/sinat_34233802/article/details/53149002