搭建SSM环境_XML版_基于maven的web项目详解

广告位

  一.基础环境 构建项目:web_xml 数据库 CREATE DATABASE webdemo0…

 

一.基础环境

构建项目:web_xml

数据库

CREATE DATABASE webdemo001; USE webdemo001; CREATE TABLE t_student(   sid INT PRIMARY KEY AUTO_INCREMENT,   student_name VARCHAR(50),   age INT ); INSERT INTO t_student(student_name,age) VALUES('jack',18); INSERT INTO t_student(student_name,age) VALUES('rose',21);

JavaBean

@Table(name = "t_student") public class Student {     @Id     private Integer sid;      @Column(name = "student_name")     private String studentName;      private Integer age;      @Override     public String toString() {         return "Student{" +                 "sid=" + sid +                 ", studentName='" + studentName + ''' +                 ", age=" + age +                 '}';     }      public Integer getSid() {         return sid;     }      public void setSid(Integer sid) {         this.sid = sid;     }      public String getStudentName() {         return studentName;     }      public void setStudentName(String studentName) {         this.studentName = studentName;     }      public Integer getAge() {         return age;     }      public void setAge(Integer age) {         this.age = age;     }      public Student() {     }      public Student(Integer sid, String studentName, Integer age) {         this.sid = sid;         this.studentName = studentName;         this.age = age;     }

添加坐标

<?xml version="1.0" encoding="UTF-8"?>  <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/xsd/maven-4.0.0.xsd">     <parent>         <artifactId>ssm_all</artifactId>         <groupId>com.czxy</groupId>         <version>1.0-SNAPSHOT</version>     </parent>     <modelVersion>4.0.0</modelVersion>      <artifactId>web_xml</artifactId>     <packaging>war</packaging>      <!-- 统一版本控制 -->     <properties>         <mysql.version>5.1.32</mysql.version>         <druid.version>1.0.31</druid.version>         <mybatis.version>3.4.6</mybatis.version>         <mapper.version>3.5.3</mapper.version>         <mybatis.spring.version>1.3.2</mybatis.spring.version>         <spring.version>5.1.6.RELEASE</spring.version>         <pagehelper.version>5.1.2</pagehelper.version>         <jsp.version>2.1</jsp.version>         <servlet.version>3.1.0</servlet.version>         <jstl.version>1.2</jstl.version>         <jackson.version>2.9.8</jackson.version>     </properties>      <dependencies>         <!-- mysql驱动 -->         <dependency>             <groupId>mysql</groupId>             <artifactId>mysql-connector-java</artifactId>             <version>${mysql.version}</version>         </dependency>         <!-- druid连接池 -->         <dependency>             <groupId>com.alibaba</groupId>             <artifactId>druid</artifactId>             <version>${druid.version}</version>         </dependency>         <!-- mybatis和通用mapper,整合jar -->         <dependency>             <groupId>org.mybatis</groupId>             <artifactId>mybatis</artifactId>             <version>${mybatis.version}</version>         </dependency>         <dependency>             <groupId>tk.mybatis</groupId>             <artifactId>mapper</artifactId>             <version>${mapper.version}</version>         </dependency>         <dependency>             <groupId>org.mybatis</groupId>             <artifactId>mybatis-spring</artifactId>             <version>${mybatis.spring.version}</version>         </dependency>         <!-- spring -->         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-context</artifactId>             <version>${spring.version}</version>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-aspects</artifactId>             <version>${spring.version}</version>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-jdbc</artifactId>             <version>${spring.version}</version>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-test</artifactId>             <version>${spring.version}</version>         </dependency>         <!-- 分页助手 -->         <dependency>             <groupId>com.github.pagehelper</groupId>             <artifactId>pagehelper</artifactId>             <version>${pagehelper.version}</version>         </dependency>         <!-- spring mvc -->         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-webmvc</artifactId>             <version>${spring.version}</version>         </dependency>         <!-- jsp/servlet/jstl -->         <dependency>             <groupId>javax.servlet.jsp</groupId>             <artifactId>jsp-api</artifactId>             <version>${jsp.version}</version>             <scope>provided</scope>         </dependency>         <dependency>             <groupId>javax.servlet</groupId>             <artifactId>javax.servlet-api</artifactId>             <version>${servlet.version}</version>             <scope>provided</scope>         </dependency>         <dependency>             <groupId>javax.servlet</groupId>             <artifactId>jstl</artifactId>             <version>${jstl.version}</version>         </dependency>         <!-- jackson spring mvc json转换工具 -->         <dependency>             <groupId>com.fasterxml.jackson.core</groupId>             <artifactId>jackson-databind</artifactId>             <version>${jackson.version}</version>         </dependency>     </dependencies>      <build>         <plugins>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-compiler-plugin</artifactId>                 <version>3.5.1</version>                 <configuration>                     <source>1.8</source>                     <target>1.8</target>                 </configuration>             </plugin>             <plugin>                 <groupId>org.apache.tomcat.maven</groupId>                 <artifactId>tomcat7-maven-plugin</artifactId>                 <version>2.2</version>                 <configuration>                     <port>8080</port>                     <path>/</path>                 </configuration>             </plugin>         </plugins>     </build> </project> 

编写db.properties下配置文件

搭建SSM环境_XML版_基于maven的web项目详解

jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/webdemo001 jdbc.username=root jdbc.password=1234

二.功能:查询所有

搭建SSM环境_XML版_基于maven的web项目详解

mapper

public interface StudentMapper {     /**      * 查询所有      * @return      */     List<Student> selectAll(); }

配置文件:MyBatis配置

搭建SSM环境_XML版_基于maven的web项目详解

  • StudentMapper.xml 用于配置Mapper
  •  namespace :用于配置mapper接口
  •  <select> 用于配置一条sql语句

StudentMapper.xml

<?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.czxy.webdemo.mapper.StudentMapper">      <sql id="studentColumns">         sid,student_name,age     </sql>     <select id="selectAll" resultType="Student">         select <include refid="studentColumns" /> from t_student     </select> </mapper>

mybatis-config.xml 用于配置mybatis相关信息

支持驼峰命名

支持PageHelper插件

mybatis-config.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> 	<settings> 		<!-- 开启下划线命名与驼峰的自动映射 --> 		<setting name="mapUnderscoreToCamelCase" value="true"/>	 	</settings> <!--	--> 	<plugins> 		<!-- 配置分页助手的插件 --> 		<plugin interceptor="com.github.pagehelper.PageInterceptor"> 			<!-- 指定数据库方言 --> 			<property name="helperDialect" value="mysql"/> 			<!-- 设置为true时,查询结果中会查询出总条数信息 --> 			<property name="rowBoundsWithCount" value="true"/> 			<!-- 分页合理化参数,pageNum<=0 时会查询第一页,pageNum>pages (超过总数时),会查询最后一页 --> 			<property name="reasonable" value="true"/> 		</plugin> 	</plugins>  </configuration>

service

public interface StudentService {     List<Student> findAll(); }

StudentServiceImpl

public class StudentServiceImpl implements StudentService {     private StudentMapper studentMapper;        @Override     public List<Student> findAll() {         return studentMapper.selectAll();     } }

配置文件:Spring 整合 MyBatis

搭建SSM环境_XML版_基于maven的web项目详解

  • applicationContext-transaction.xml 事务相关配置
  • applicationContext-mybatis.xml 整合mybatis相关配置 + mapper配置
  • applicationContext.xml 连接池相关配置 + service配置
  • web.xml 用于加载spring相关的所有xml文件

applicationContext-transaction.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:tx="http://www.springframework.org/schema/tx"        xmlns:aop="http://www.springframework.org/schema/aop"        xsi:schemaLocation="http://www.springframework.org/schema/beans                             http://www.springframework.org/schema/beans/spring-beans.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                             ">      <!-- 定义事务管理器 -->     <bean id="transactionManager"           class="org.springframework.jdbc.datasource.DataSourceTransactionManager">         <property name="dataSource" ref="dataSource" />     </bean>      <!-- 定义事务策略 -->     <tx:advice id="txAdvice" transaction-manager="transactionManager">         <tx:attributes>             <!--所有以query开头的方法都是只读的 -->             <tx:method name="find*" read-only="true"/>             <!--其他方法使用默认事务策略 -->             <tx:method name="*" />         </tx:attributes>     </tx:advice>      <aop:config>         <!--pointcut元素定义一个切入点,execution中的第一个星号 用以匹配方法的返回类型,             这里星号表明匹配所有返回类型。 com.abc.dao.*.*(..)表明匹配cn.itcast.mybatis.service包下的所有类的所有             方法 -->         <aop:pointcut id="myPointcut" expression="execution(* com.czxy.webdemo.service..*.*(..))" />         <!--将定义好的事务处理策略应用到上述的切入点 -->         <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" />     </aop:config>  </beans>

applicationContext-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"        xsi:schemaLocation="http://www.springframework.org/schema/beans                             http://www.springframework.org/schema/beans/spring-beans.xsd">      <!-- 配置SqlSessionFactory -->     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">         <!-- 全局配置文件的地址 -->         <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />         <!-- 数据源 -->         <property name="dataSource" ref="dataSource" />         <!-- mapper映射文件的扫描 -->         <property name="mapperLocations" value="classpath:mybatis/mappers/*.xml" />         <!-- 别名扫描包 -->         <property name="typeAliasesPackage" value="com.czxy.webdemo.domain" />     </bean>      <!-- Mapper接口的扫描 -->     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">         <!-- 扫描mapper接口的包 -->         <property name="basePackage" value="com.czxy.webdemo.mapper"/>     </bean>  </beans>

applicationContext.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:context="http://www.springframework.org/schema/context"        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                             ">     <!--加载配置文件-->     <context:property-placeholder location="classpath:db.properties"/>      <!-- 配置连接池 -->     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"           init-method="init" destroy-method="close">         <property name="url" value="${jdbc.url}" />         <property name="username" value="${jdbc.username}" />         <property name="password" value="${jdbc.password}" />     </bean>      <!--配置bean-->     <bean id="studentService" class="com.czxy.webdemo.service.impl.StudentServiceImpl">         <property name="studentMapper" ref="studentMapper"/>     </bean>  </beans>

修改 service 实现类,添加setter方法

public class StudentServiceImpl implements StudentService {     private StudentMapper studentMapper;      public void setStudentMapper(StudentMapper studentMapper) {         this.studentMapper = studentMapper;     }     @Override     public List<Student> findAll() {         return studentMapper.selectAll();     } }

web.xml(注意:2.5版本适合xml)

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xmlns="http://java.sun.com/xml/ns/javaee"          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"          id="MyWebApp" version="2.5">      <!-- 初始化参数,配置Spring核心配置文件位置 -->     <context-param>         <param-name>contextConfigLocation</param-name>         <param-value>classpath:spring/applicationContext*.xml</param-value>     </context-param>     <!-- Spring的ApplicationContext的加载 -->     <listener>         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>     </listener>      <!--post请求中文乱码处理-->     <filter>         <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>         <url-pattern>/*</url-pattern>     </filter-mapping>      <!-- 配置SpringMVC -->     <servlet>         <servlet-name>springmvc</servlet-name>         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>         <!-- 初始化参数,指定SpringMVC的配置文件路径,不再找WEB-INF下的默认配置 -->         <init-param>             <param-name>contextConfigLocation</param-name>             <param-value>classpath:spring/springmvc.xml</param-value>         </init-param>         <!-- 0:第一次被调用时创建; 1:Tomcat启动时就创建 -->         <load-on-startup>1</load-on-startup>     </servlet>     <servlet-mapping>         <servlet-name>springmvc</servlet-name>         <!-- 拦截除了JSP以外的所有路径,不用再写.do了 -->         <url-pattern>/</url-pattern>     </servlet-mapping>   </web-app> 

​​​​​​​配置文件:web整合spring

搭建SSM环境_XML版_基于maven的web项目详解

springmvc.xml controller配置以及访问路径相关配置

web.xml 加载spring mvc 配置文件

修改 controller,SimpleUrlHandlerMapping只能处理简单的映射,controller实现类必须实现Controller接口

springmvc.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: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                             https://www.springframework.org/schema/mvc/spring-mvc.xsd                             ">   	<!-- 视图解析器 --> 	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 		<property name="prefix" value="/jsp/" /> 		<property name="suffix" value=".jsp" /> 	</bean>  	<!--简单路径--> 	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 		<property name="mappings" > 			<props> 				<prop key="/student">studentController</prop> 			</props> 		</property> 	</bean>  	<bean id="studentController" class="com.czxy.webdemo.controller.StudentController"> 		<property name="studentService" ref="studentService" /> 	</bean>  	<!-- 将静态资源交给tomcat默认处理 	<mvc:default-servlet-handler/> 	-->  </beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xmlns="http://java.sun.com/xml/ns/javaee"          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"          id="MyWebApp" version="2.5">      <!-- 初始化参数,配置Spring核心配置文件位置 -->     <context-param>         <param-name>contextConfigLocation</param-name>         <param-value>classpath:spring/applicationContext*.xml</param-value>     </context-param>     <!-- Spring的ApplicationContext的加载 -->     <listener>         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>     </listener>      <!--post请求中文乱码处理-->     <filter>         <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>         <url-pattern>/*</url-pattern>     </filter-mapping>      <!-- 配置SpringMVC -->     <servlet>         <servlet-name>springmvc</servlet-name>         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>         <!-- 初始化参数,指定SpringMVC的配置文件路径,不再找WEB-INF下的默认配置 -->         <init-param>             <param-name>contextConfigLocation</param-name>             <param-value>classpath:spring/springmvc.xml</param-value>         </init-param>         <!-- 0:第一次被调用时创建; 1:Tomcat启动时就创建 -->         <load-on-startup>1</load-on-startup>     </servlet>     <servlet-mapping>         <servlet-name>springmvc</servlet-name>         <!-- 拦截除了JSP以外的所有路径,不用再写.do了 -->         <url-pattern>/</url-pattern>     </servlet-mapping>   </web-app> 

controller

public class StudentController implements Controller {  private StudentService studentService;  public void setStudentService(StudentService studentService) {     this.studentService = studentService; }      @Override     public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {         ModelAndView modelAndView = new ModelAndView();         // 1.设置数据         List<Student> list = studentService.findAll();         modelAndView.addObject("list", list);          modelAndView.setViewName("student_list");         return modelAndView;     } }

到这里就搭建完成了噢~~

看完恭喜你,又知道了一点点!!!

你知道的越多,不知道的越多! 

~感谢志同道合的你阅读,  你的支持是我学习的最大动力 ! 加油 ,陌生人一起努力,共勉!!

情葬死海

关于作者: 情葬死海

为您推荐