温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SSM框架搭建之maven方式(二)

发布时间:2020-07-28 04:28:18 来源:网络 阅读:454 作者:wx5d8484d25f876 栏目:编程语言

一、MAVEN下载安装


maven的下载地址:http://maven.apache.org/download.cgi

SSM框架搭建之maven方式(二)

解压之后路径 D:\java\apache-maven-3.6.2;
新增系统变量【MAVEN_HOME】,变量名替换成自己本地解压路径

SSM框架搭建之maven方式(二)

系统变量【Path】追加值:%MAVEN_HOME%\bin

SSM框架搭建之maven方式(二)

运行cmd,输入mvn –version或者mvn –v;如果看见下图则说明maven安装配置完毕

SSM框架搭建之maven方式(二)

本地仓储配置文件:D:\java\apache-maven-3.6.2\conf\settings.xml
在maven目录下创建本地存储文件夹repository,设置本地存储路径 D:\java\apache-maven-3.6.2\repository

<localRepository>D:\java\apache-maven-3.6.2\repository</localRepository>

直接访问maven<localRepository>D:\java\apache-maven-3.6.2\repository</localRepository>公共仓库的速度比较慢,所以推荐使用阿里的maven仓库镜像,在mirrors节点下,新增如下内容

<mirror>
          <id>aliyun</id>
          <name>aliyun</name>
          <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
          <mirrorOf>central</mirrorOf>
        </mirror>

设置默认jdk,在profiles节点下,新增如下内容(可以省略,在pom.xml配置也行)

<profile>
        <id>nexus</id>
        <repositories>
            <repository>
                <id>central</id>
                <url>http://repo.maven.apache.org/maven2</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
            <repository>
                <id>ansj-repo</id>
                <name>ansj Repository</name>
                <url>http://maven.nlpcn.org/</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>
    </profile>

二、MAVEN配置


Maven项目目录结构
|----src
| |----main
| | |----java —— java文件
| | |----resource ——资源文件
| | |----webapp ——jsp/html/css/image/js文件
| |----test
| | |----java ——测试java文件
| | |----resource ——资源文件
|----target ——项目输出位置
|----pom.xml ——maven项目的标志


eclipse设置Maven
在Window----Preferences中找到Maven节点----User Setting,设置如下图:

SSM框架搭建之maven方式(二)

点击Installations节点,添加maven runtime如下图所示

SSM框架搭建之maven方式(二)

三、eclipse创建Maven工程


点击File----New----Maven Project(默认有的话)或者File----New----Other,选择maven文件夹,点击Maven Project如下图所示

SSM框架搭建之maven方式(二)

点击Next,选择默认工作空间位置,选择web类型,再点击Next

SSM框架搭建之maven方式(二)

SSM框架搭建之maven方式(二)

填写GroupID、ArtifactID,点击Finish完成maven项目web工程的创建
Group ID:相当于一个组织
Artifact ID:相当于这个组织下的一个具体项目
Packege:根据Group ID和Artifact ID生成一个默认的名称

SSM框架搭建之maven方式(二)

四、配置的修改


在项目上右键,查看项目信息,默认的Dynamic Web Module为2.3,使用Tomcat 8.5,需要修改为3.1

SSM框架搭建之maven方式(二)

(1).找到maven工程所在目录下org.eclipse.wst.common.project.facet.core.xml此文件

SSM框架搭建之maven方式(二)

SSM框架搭建之maven方式(二)

(2).maven工程下的web.xml文件替换为

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
       version="3.1" metadata-complete="true">
</web-app>

(3).pom.xml文件中修改build节点,添加如下内容

<plugins>
       <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <configuration>
                     <source>1.8</source>
                     <target>1.8</target>
              </configuration>
       </plugin>
    </plugins>

(4).在项目上右键,找到Maven属性下的Update Project

SSM框架搭建之maven方式(二)

(5).选择该项目进行更新,如果怕不能强制更新,可以勾选Force Update of Snapshots/Releases,更新完毕再一次查看Dynamic Web Module是否为3.1

SSM框架搭建之maven方式(二)

右键项目--Properties--Deployment Assembly,移除target

SSM框架搭建之maven方式(二)

五、案例测试


目录结构

SSM框架搭建之maven方式(二)

  1. Pom.xml再添加如下内容
<properties>
        <srping.version>4.0.2.RELEASE</srping.version>
        <mybatis.version>3.2.8</mybatis.version>
        <slf4j.version>1.7.12</slf4j.version>
        <log4j.version>1.2.17</log4j.version>
    </properties>
    <dependencies>
        <!-- spring框架包 start -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${srping.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${srping.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${srping.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${srping.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${srping.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${srping.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.geronimo.bundles</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.8_2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${srping.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${srping.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${srping.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${srping.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${srping.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${srping.version}</version>
        </dependency>
        <!-- spring框架包 end -->

        <!-- mybatis框架包 start -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <!-- mybatis框架包 end -->

        <!-- 数据库驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.17</version>
        </dependency>
        <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

        <!-- jstl标签类 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- log start -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <!-- log END -->
        <!-- Json -->
        <!-- 格式化对象,方便输出日志 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.6</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <!-- 上传组件包 start -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.10</version>
        </dependency>
        <!-- 上传组件包 end -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- java ee包 -->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

2.datasource.properties
mysql8的驱动名称不是com.mysql.jdbc.Driver;格外注意url的写法

datasource.driverClassName=com.mysql.cj.jdbc.Driver
datasource.userName=root
datasource.userPassword=123456
datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC

3. log4j.properties

#定义LOG输出级别
log4j.rootLogger=INFO,Console,File

#定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活的指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c]-%m%n

#mybatis显示SQL语句日志配置     net.cxp.blog.dao是你的dao接口所在的包名  
#log4j.logger.org.mybatis=DEBUG
log4j.logger.com.lymn.it.mapper=DEBUG

#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File=org.apache.log4j.RollingFileAppender
#指定输出目录
log4j.appender.File.File=d:/logs/ssm.log
#定义文件最大大小
log4j.appender.File.MaxFileSize=10MB
#输出所有日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold=DEBUG
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm|\:ss}][%c]%m%n

4. spring-mvc.xml

<?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:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="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.xsd    
                        http://www.springframework.org/schema/mvc    
                        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器,此处必须排除掉@Service组件 -->
    <context:component-scan base-package="com.lymn.it.controller" />
    <!-- 会自动注册RequestMappingHandlerMapping与RequestMappingHandlerAdapter两个Bean,这是Spring MVC
    为@Controller分发请求所必需的。启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
    <mvc:annotation-driven/>

    <!-- 视图解析器 -->
    <!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!--对静态资源文件的访问 -->
    <!-- 针对springMVC的restful风格的url而言,配置了 -->
    <mvc:resources mapping="/images/**" location="/WEB-INF/images/" />
    <mvc:resources mapping="/css/**" location="/WEB-INF/css/" />

</beans>

5.spring-mybatis.xml

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="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.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop.xsd
                        http://www.springframework.org/schema/task 
                        http://www.springframework.org/schema/task/spring-task.xsd">

    <!-- 配置包含注解的扫描路径,但是不包括controller -->
    <context:component-scan base-package="com.lymn.it.*">
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <!-- PropertyPlaceholderConfigurer 是 BeanFactory 后置处理器的实现,也是 BeanFactoryPostProcessor 
    接口的一个实现。 允许将上下文(配置文件)中的属性值放在另一个单独的标准 JavaProperties 文件中去。在 XML 文件中用类似 EL 
    表达式的 ${key} 替换指定的 properties 文件中的值 -->
    <!-- <bean id="placeHolder"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:config/datasource.properties"></property>
        多个数据源写法
        <property name="locations">
            <list>
                <value>classpath:config/datasource1.properties</value>
                <value>classpath:config/datasource2.properties</value>
            </list>
        </property>
    </bean> -->

    <!-- Spring2.5 以后,引入了简化的引入外部文件的方式 -->
    <context:property-placeholder location="classpath:datasource.properties" />
    <!-- Spring提供了的数据源,每个连接请求时都新建一个连接 -->
     <bean id="dataSource" 
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${datasource.driverClassName}"></property>
        <property name="url" value="${datasource.url}"></property>
        <property name="username" value="${datasource.userName}"></property>
        <property name="password" value="${datasource.userPassword}"></property>
    </bean>

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 扫描entity包 使用别名 -->
        <property name="typeAliasesPackage" value="com.lymn.it.model"></property>
        <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
        <!-- <property name="configLocation" value="classpath:mybatis-config.xml" /> -->
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:com/lymn/it/mapper/*.xml"></property>
    </bean>
    <!-- mapper接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.lymn.it.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <!-- (事务管理)transaction manager -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 事务增强,这里用的是spring提供的特性 -->
    <tx:advice id="tx" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="select*" read-only="true" />
            <tx:method name="find*" read-only="true" />
            <tx:method name="get*" read-only="true" />
            <tx:method name="has*" read-only="true" />
            <tx:method name="count*" read-only="true" />
            <tx:method name="search*" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="pc"
            expression="execution(* com.lymn.it.service.impl..*.*(..))" />
        <!--把事务控制在Service层 -->
        <aop:advisor pointcut-ref="pc" advice-ref="tx" />
    </aop:config>

</beans>

6.model--User

package com.lymn.it.model;

public class User {
        private Integer userid;
        private String username;
        private String password;
        private String email;
        private String phone;
        private Integer status;
        private String code;
        public Integer getUserid() {
            return userid;
        }
        public void setUserid(Integer userid) {
            this.userid = userid;
        }
        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 getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }
        public String getPhone() {
            return phone;
        }
        public void setPhone(String phone) {
            this.phone = phone;
        }
        public Integer getStatus() {
            return status;
        }
        public void setStatus(Integer status) {
            this.status = status;
        }
        public String getCode() {
            return code;
        }
        public void setCode(String code) {
            this.code = code;
        }
        @Override
        public String toString() {
            return "User [userid=" + userid + ", username=" + username + ", password=" + password + ", email=" + email
                    + ", phone=" + phone + ", status=" + status + ", code=" + code + "]";
        }

}

7.mapper-UserMapper.java

package com.lymn.it.mapper;

import java.util.List;

import com.lymn.it.model.User;

public interface UserMapper {
    public List<User> getAllUsers();

    public User getUserById(int userid);

    public boolean deleteUserById(int userid);

    public boolean insertUser(User user);

    public boolean updateUserById(User user);
}

mapper-UserMapper.java

<?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.lymn.it.mapper.UserMapper">
    <select id="getAllUsers" resultType="User">
        SELECT * FROM user
    </select>
    <select id="getUserById" parameterType="int" resultType="User">
        SELECT *
        FROM user WHERE userid = #{userid}
    </select>
    <insert id="insertUser" parameterType="User"
        useGeneratedKeys="true" keyProperty="userid">
        INSERT INTO s_user(username,password,email,phone)
        VALUES(#{username},#{password},#{email},#{phone})
    </insert>
    <update id="updateUserById" parameterType="User">
        UPDATE user SET username = #{username},
                          password = #{password},
                          email = #{email},
                          phone = #{phone}
                          WHERE 
                          userid = #{userid}
    </update>
    <delete id="deleteUserById" parameterType="int">
        DELETE  FROM user WHERE userid = #{userid}
    </delete>
</mapper>

8.service-UserService.java

package com.lymn.it.service;

import java.util.List;

import com.lymn.it.model.User;

public interface UserService {
    public List<User> getAllUsers();

    public User getUserById(int userid);

    public boolean deleteUserById(int userid);

    public boolean insertUser(User user);

    public boolean updateUserById(User user);
}

service-UserServiceImpl.java

package com.lymn.it.service.impl;

import java.util.List;

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

import com.lymn.it.mapper.UserMapper;
import com.lymn.it.model.User;
import com.lymn.it.service.UserService;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public List<User> getAllUsers() {
        return userMapper.getAllUsers();

    }

    @Override
    public User getUserById(int userid) {
        return userMapper.getUserById(userid);

    }

    @Override
    public boolean deleteUserById(int userid) {
        return userMapper.deleteUserById(userid);

    }

    @Override
    public boolean insertUser(User user) {
        return userMapper.insertUser(user);
    }

    @Override
    public boolean updateUserById(User user) {
        return userMapper.updateUserById(user);
    }

}

9.controller-UserController

package com.lymn.it.controller;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lymn.it.model.User;
import com.lymn.it.service.UserService;

@Controller
public class UserController {
    @Autowired
    UserService userService;
    Logger logger=Logger.getLogger(UserController.class);
    @RequestMapping(value="/user")
    public String user(Map<Object,Object> map) {
        logger.info("查询所有用户数据");
        List<User> userList =  userService.getAllUsers();
        map.put("user", userList);
        logger.info("查询完毕,返回页面");
        return "user";
    }

}

10.web.xml添加如下配置

<!-- spring容器配置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!-- 注意,spring加载配置文件 -->
        <param-value>
            classpath:spring-mybatis.xml
        </param-value>
    </context-param>
    <!-- spring容器监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--springMVC的核心分发器 -->
    <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定Spring的配置文件 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <!-- 启动加载一次 -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <!-- 此处可以可以配置成*.do 适配Struts的习惯 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 防止Spring内存溢出监听器 -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>

    <!-- 解决工程编码过滤器 -->
    <filter>
        <filter-name>encodingFilter</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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

11.jsp/user.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User</title>
</head>
<body>
    <h2>${requestScope.user}</h2>
</body>

12.建数据库test,然后在创建表

CREATE TABLE `user` (
  `userid` INT(11) NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(255) DEFAULT NULL,
  `password` VARCHAR(255) DEFAULT NULL,
  `email` VARCHAR(255) DEFAULT NULL,
  `phone` VARCHAR(255) DEFAULT NULL,
  `status` VARCHAR(255) NOT NULL DEFAULT '0',
  `code` VARCHAR(255) DEFAULT NULL,
  PRIMARY KEY (`userid`)
) ENGINE=INNODB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;

13.项目部署

配置 Tomcat 访问权限,tomcat中tomcat-user.xml的配置(tomcat根目录/conf/tomcat-users.xml)

<role rolename="manager-gui"/> 
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user password="123456" username="root"
roles="manager-gui,manager-script,manager-jmx,manager-status" />

双击 tomcat 解压包中 bin 目录下的 startup.bat 命令进行启动Tomcat服务器。在浏览器地址来中进行访问http://127.0.0.1:8080/manager/

SSM框架搭建之maven方式(二)

输入用户名root,密码123456进行登录,如果顺利则请进入下一步。

SSM框架搭建之maven方式(二)

配置maven的setting.xml
在 conf/setting.xml 文件中的标签 <servers> 添加如下内容

 <server>
       <!--通过标签名字让 maven 去关联我们的 Tomcat 服务器--> 
        <id>tomcat8</id>
        <!--username 和 password 一定要和 tomcat 中的 tomcat_user.xml 中一致-->
        <username>root</username>
        <password>123456</password>
    </server>

在 pom.xml 文件的plugins子节点中添加如下内容

<plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <url>http://127.0.0.1:8080/manager/text</url>
                    <!--关联id-->
                    <server>tomcat8</server>
                    <username>root</username>
                    <password>123456</password>
                    <!--访问的路径(项目名)-->
                    <path>/ssm01</path>
                    <update>true</update>
                </configuration>
            </plugin>

先运行Tomcat目录下/bin/startup.bat
然后右击项目Run As–Maven build,在Goals中输入:clean install tomcat7:redeploy

访问如下图表示配置成功
SSM框架搭建之maven方式(二)

附:maven命令
执行命令
1)Run as → clean install
2)Run as → tomcat7:deploy 注:第1次部署执行
3)Run as → tomcat7:redeploy 注:第2次或以后需要重新发布执行
4)Run as → tomcat7:run 注:部署到 tomcat 中启动

在此过程中遇到的错误

SSM框架搭建之maven方式(二)

启动tomcat,在进行编译构建

SSM框架搭建之maven方式(二)

用Mybatis去操作底层数据库的时候,需要用到xml配置文件,一般我们是把配置文件和dao放置在同一层目录。操作maven项目的时候,我们可能会遇到无法读取到dao对应的mapper.xml文件。在pom.xml的build节点中添加如下内容

<resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
</resources>
或者
<plugin>  
            <groupId>org.codehaus.mojo</groupId>  
            <artifactId>build-helper-maven-plugin</artifactId>  
            <version>1.8</version>  
            <executions>  
                <execution>  
                    <id>add-resource</id>  
                    <phase>generate-resources</phase>  
                    <goals>  
                        <goal>add-resource</goal>  
                    </goals>  
                    <configuration>  
                        <resources>  
                            <resource>  
                                <directory>src/main/java</directory>  
                                <includes>  
                                    <include>**/*.xml</include>  
                                </includes>  
                            </resource>  
                        </resources>  
                    </configuration>  
                </execution>  
            </executions>  
</plugin>  
再或者
<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <id>add-resource</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>add-resource</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>src/main/java</directory>
                                    <includes>
                                        <include>**/*.xml</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
</plugin>
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI