農農  

SSM整合

1. 登录案例

1.1 创建实体类

@Repository
public class User {
    private  Integer uid;
    private  String userName;
    private  String password;
    private  String realName;

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }
}

 

 

 

 

1.2 编写Dao层接口

public interface UserDao {
    //登录
    public User Login(@Param("userName") String userName, @Param("password") String password);
}

 

 

 

 

 

1.3 编写小配置文件

<mapper namespace="com.invoicing.dao.UserDao">
    <select id="Login" resultType="com.invoicing.entity.User">
        select * from users where userName=#{userName} and password=#{password}
    </select>
</mapper>

 

 

 

1.4 编写application-mybatis.xml文件

   

<!--加载属性文件-->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

        <property name="locations" >
            <array>
                <value>classpath*:*.properties</value>
            </array>
        </property>
    </bean>

<!--DataSource-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>
<!--SessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
 <!--   <property name="mapperLocations" value="classpath*:com/invoicing/dao/*.xml"></property>-->
    <property name="configLocation" value="classpath:config.xml"></property>
</bean>

<!--包扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.invoicing.dao"></property>
 </bean>

<!--事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>
<!--开启事务注解配置-->
<tx:annotation-driven/>
<context:annotation-config/>
<context:component-scan base-package="com.invoicing"></context:component-scan>

 

 

 

 

 

1.5 编写Config.xml文件

<settings>
    <setting name="logImpl" value="LOG4J"/>
</settings>
<!-- 引入 pageHelper插件 -->
<!--注意这里要写成PageInterceptor, 5.0之前的版本都是写PageHelper, 5.0之后要换成PageInterceptor-->
<plugins>
    <plugin interceptor="com.github.pagehelper.PageHelper"></plugin>
</plugins>
<mappers>
    <package name="com.invoicing.dao"/>
</mappers>

 

 

 

 

1.6 编写jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/invoicingsystem?useUniCode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123

 

 

 

1.7编写Spring-mvc.xml文件

<!--推荐使用注解方式-->
<!--配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/jsp/"></property>
    <!--return "index"======>/jsp/index.jsp-->
    <property name="suffix" value=".jsp"></property>
</bean>

<!--开启MVC的注解配置-->
<mvc:annotation-driven/>

<!--包扫描器:扫描 MVC中的注解-->
<context:component-scan base-package="com.invoicing"/>


<mvc:default-servlet-handler/>

 

 

 

 

 

1.7 编写web.xml文件

<servlet>
  <servlet-name>SpringMVC</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!--读取spring-mvc.xml文件-->
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-mvc.xml</param-value>
  </init-param>
</servlet>

<servlet-mapping>
  <servlet-name>SpringMVC</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

<!--spring和MyBatis整合的核心配置-->
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:application-*.xml</param-value>
</context-param>
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

 

 

 

 

1.8  编写Service层接口

public interface UserService {
    //登录
    public User Login(@Param("userName") String userName, @Param("password") String password);
}

 

 

 

 

1.9 编写Service层接口实现类

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;
    @Override
    public User Login(String userName, String password) {
        return userDao.Login(userName,password);
    }
}

 

 

 

 

 

 

1.10 编写Contrller

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public ModelAndView login(String userName, String password, HttpServletResponse respones,HttpServletRequest request, ModelAndView mv){
        System.out.println(userName+"\t"+password);
        User login = userService.Login(userName, password);
        if (login!=null){
            System.out.println("登陆成功");
            request.getSession().setAttribute("login",login);
            mv.setViewName("index");
        }else {

            mv.setViewName("login");
        }
        return mv;
    }

}

 

 

 

 

1.11 编写过滤器实现非法拦截

@WebFilter(urlPatterns = {"/*"})
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("======过滤器====");
        HttpServletRequest request=(HttpServletRequest)  servletRequest;
        if(request.getRequestURI().equals("/user/login")){
            filterChain.doFilter(servletRequest,servletResponse);
        }
        Object login = request.getSession().getAttribute("login");
        if (login==null){
            request.getRequestDispatcher("/jsp/login.jsp").forward(servletRequest,servletResponse);
        }else{
            filterChain.doFilter(servletRequest,servletResponse);
        }
    }

   @Override
    public void destroy() {

    }
}

 

 

 

 

posted on 2019-11-14 16:17  Baekhyunne  阅读(165)  评论(0编辑  收藏  举报