菜单

SSM整合笔记

2019年5月12日 - 皇家前端

  D:service层

 

package com.ssm.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ssm.dao.UserDao;
import com.ssm.view.User;

@Service("userService")
public class UserService {
    @Autowired
    private UserDao userDao;
    /**
     * 登录操作
     * @param user_name
     * @param user_password
     * @return
     */
    public User login(String user_name,String user_password) {
        return userDao.login(user_name, user_password);
    }
}

     找到src\main\webapp\WEB-INF目录下的web.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>
    <!-- 引入分页插件 -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!-- 分页参数合理化 -->
            <property name="reasonable" value="true"/>
        </plugin>
    </plugins>
</configuration>

      详细分解

  A:数据库

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    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-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">


    <!-- 启用注解驱动 -->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!-- 处理静态资源 -->
    <mvc:default-servlet-handler />
    <!-- 开启组件扫描 -->
    <context:component-scan
        base-package="com.junge.*" />
    <!-- 配置视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/page/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 拦截器 -->
     <mvc:interceptors>
        <!-- 多个拦截器顺序执行
        登录认证拦截器 -->
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <bean class="com.junge.interceptor.Interceptor" />
        </mvc:interceptor>
    </mvc:interceptors>  

    <!--全局异常处理 只要你实现了HandelExceptionResolver接口 , 这个类就是一个全局异常处理类 -->
    <bean class="com.junge.exception.JExceptionResolver"></bean>
    <!-- 文件上传 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设置默认编码 -->
        <property name="defaultEncoding" value="utf-8"></property>
        <!-- 上传文件总的大小最大为5M -->
        <property name="maxUploadSize" value="5242440"></property>
    </bean>
</beans>
    

  图片 1

  在创造登入成功后的页面

   在事先成立好的dao包中开创UserDao接口
并写登入的方法

  ps:假诺创设完maven项目事后项目报错,或许是从未安插汤姆cat

    在src\main\resources目录下开创spring-mvc整合配置文件
spring-mvc.xml

     图片 2

 

package com.ssm.dao;

import org.apache.ibatis.annotations.Param;

import com.ssm.view.User;

public interface UserDao {
    /**
     * 登录操作
     * @param user_name        用户名
     * @param user_password        密码
     * @return
     */
    public User login(@Param("user_name")String user_name,@Param("user_password")String user_password);
}

 

  C:dao层(接口)

     图片 3

        图片 4

    填写配置内容

    A:包目录

<%@ page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录成功</title>
</head>
<body>
        hello
</body>
</html>

    创造三个user表(oracle数据Curry面user是注重字)

 


 

     —————————————————————————————-

    Mysql的配置  

    在前头创建好的service包中创建UserService类 
 写登陆的点子

 

      

      详细说明

    在前头成立好的action包里创造三个UserAction类


 

<%@ page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录页面</title>
</head>
<body>
        <form action="#" method="post">
            账号:<input type="text" id="user_name" name="user_name"><br>
            密码:<input type="password" id="user_password" name="user_password"><br>
            <input type="submit" value="login">
        </form>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
 "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">

<mapper namespace="com.ssm.dao.UserDao"><!-- 对应的dao包的类 -->
    <!-- 建立sql查询结果字段与实体属性的映射关系 -->
    <resultMap id="UserMap" type="com.ssm.view.User"><!-- 对应的实体类的路径 -->
        <result column="user_id" property="user_id" />

        <result column="user_name" property="user_name" />
        <result column="user_password" property="user_password" />
    </resultMap>

    <select id="login" resultMap="UserMap">
        SELECT
            *
        FROM
            user
        WHERE
            user_name=#{user_name} and user_password=#{user_password}
    </select>


</mapper>

    用来分页的安插文件(pagehelper)

*    图片 5*

      在src\main\webapp下创立一个索引

 

      jdbc.driver= com.mysql.jdbc.Driver;//数据库驱动

      jdbc.url =jdbc:mysql://localhost:3306/blog?characterEncoding=utf-8 //数据库地址(一般写ip地址)

      jdbc.username = root  //数据库用户名

      jdbc.password =root   //数据库密码

jdbc.driver= com.mysql.jdbc.Driver
jdbc.url =jdbc:mysql://localhost:3306/blog?characterEncoding=utf-8
jdbc.username = root
jdbc.password =root

    源代码

 

    —————————————————————-

      

        (该目录的名字随便取,该目录下存放页面文件
         WEB-INF目录是不可知的,所以其下的文书是安全的
         一般来说存放的是亟需报到后本事够访问的后台管理页面)

    —————————————————————-

      

     

 

    C:静态能源目录

package com.ssm.view;

public class User {
    private int user_id;
    private String user_name;
    private String user_password;
    public int getUser_id() {
        return user_id;
    }
    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }
    public String getUser_name() {
        return user_name;
    }
    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }
    public String getUser_password() {
        return user_password;
    }
    public void setUser_password(String user_password) {
        this.user_password = user_password;
    }

}

  源代码

  创建maven项目经过大致

  B:spring-mybatis

    ——————–

二,在pom.xml里面导入相应的jar的依附

  E:mybatis-config.xml

  B:实体类(view)

    

       图片 6

 

     图片 7

 

*     图片 8*

四,文件配置

    D:暗中认可首页

    图片 9

 

      图片 10

 

    —————————————————————-

  G:访问(省略)

  A:计划数据库数据

      图片 11

      在src\main\webapp\WEB-INF下成立贰个索引

 

    在src\main\resources目录下成立spring-mybatis整合配置文件
spring-mybatis.xml

      图片 12

    在在此以前创制好的view包中开创User实体类

      在src\main\webapp下创办3个 index.jsp
文件

    图片 13

伍,访问测试

      表数据

  F:控制层(action/controller)

 

  D:web.xml

      图片 14

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <!-- 字符过滤,防止post请求乱码 -->
    <filter>
        <filter-name>SpringCharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>SpringCharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!-- spring mvc 请求响应 -->
    <!-- The front controller of this Spring Web application, responsible for 
        handling all application requests -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-*.xml</param-value><!-- 这么写会自动找到之前写的 spring-mybaits文件和spring-mvc文件 -->
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>*.json</url-pattern>
    </servlet-mapping>
    <!-- 表示后台控制层接受。action和json结尾的请求 本项目中利用action请求转发页面 json请求转发json数据 -->
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    <!-- 配置静态资源文件路径 -->
    <servlet-mapping>
        <servlet-name>default</servlet-name><!-- 表示static目录下的文件都是静态文件 -->
        <url-pattern>/static/*</url-pattern><!-- 不配置的话,框架会认为这些文件的路径都是请求,导致无法访问 -->
    </servlet-mapping>

    <!-- 配置错误页面 -->
    <error-page>
        <error-code>403</error-code>
        <location>/WEB-INF/page/portal/403.jsp</location>
    </error-page>
    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/page/portal/404.jsp</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/WEB-INF/page/portal/500.jsp</location>
    </error-page>
</web-app>

    

  E:妄想页面(登陆)

    ——————–

 

1,创建maven项目

 

    在src\main\resources目录下创办数据库连接配置文件
db.properties(new一个file)

      详细解释

 

       依据mvc分层观念,创设包目录

    创建xml文件 
名称为UserDaomapper.xml

    在src/main/resources/路线下创办mapper文件夹 
用来放mybatis dao的安顿文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

    <!-- 引入数据库配置文件 -->
    <!-- <util:properties id="config"
        ></util:properties> -->
    <context:property-placeholder  location="classpath:db.properties"/>
    <!-- 配置数据连接参数及连接池 -->
    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}"></property>
    </bean>
<!-- spring 集成mybaits 所以不用配置mybatis -->
<!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 注入连接池 -->
    <property name="dataSource" ref="dataSource"/>
    <!-- 映射文件的位置 -->
    <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    <!-- 分页插件pageHelper -->
    <property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>

<!-- 该bean负责调用sqlsession的getMaper函数 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.junge.dao*"/>
    <!--  -->
    <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property> -->
</bean>
<!-- 开启事务注解驱动 -->
<tx:annotation-driven/>
<!-- 事务管理 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>

</beans>

  C:spring-mvc

    B:后台页面目录

*     图片 15*

     —————————————————————————————

3,营造主旨目录

   从前创制的index页面

    图片 16

SSM整合笔记

    —————————————————————-

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.junge</groupId>
    <artifactId>SSM1</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>SSM1 Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- 导入springmvc -->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.0.RELEASE</version>
        </dependency>

        <!-- spring-jdbc -->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.0.0.RELEASE</version>
        </dependency>
        <!-- mybatis -->
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.3</version>
        </dependency>

        <!-- 连接池commons-dbcp -->
        <!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>


        <!-- 数据库驱动 mysql -->
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
        </dependency>

        <!-- mybatis-spring -->
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.2</version>
        </dependency>

        <!-- jstl -->
        <!-- https://mvnrepository.com/artifact/jstl/jstl -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- js -->
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.2</version>
        </dependency>

        <!-- log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!-- 文件上传 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3</version>
        </dependency>
        <!-- 引入PageHelper分页插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>


    </dependencies>
    <build>
        <finalName>SSM1</finalName>
    </build>
</project>

 

 

  

      oracle、mysql、sql
server,三者的写法均分化变量名能够随意取,可是要注意,无法和系统内置的变量名起抵触等号右侧的内容,不要加双引号或单引号

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图