• ssm_crud 搭建项目


    搭建项目,主要内容。全都已每一步截个图,具体内容git上查看:https://gitee.com/gaap/ssm-crud

    1.maven单项目,war方式

    2.git上传

    a.new >maven

    eclipse > new > maven  project > 填写信息

    这里补充一点:maven 建立的war项目,都需要配置下。右击,项目,属性 ,

    b. pom.xml

    <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">
    	<modelVersion>4.0.0</modelVersion>
    	<groupId>com.taotao.ssm</groupId>
    	<artifactId>ssm-crud</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>war</packaging>
    
    
    	<dependencies>
    		<!--引入pageHelper分页插件 -->
    		<dependency>
    			<groupId>com.github.pagehelper</groupId>
    			<artifactId>pagehelper</artifactId>
    			<version>5.0.0</version>
    		</dependency>
    
    		<!-- MBG -->
    		<dependency>
    			<groupId>org.mybatis.generator</groupId>
    			<artifactId>mybatis-generator-core</artifactId>
    			<version>1.3.5</version>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-webmvc</artifactId>
    			<version>4.3.7.RELEASE</version>
    		</dependency>
    
    		<!-- 返回json字符串的支持 -->
    		<dependency>
    			<groupId>com.fasterxml.jackson.core</groupId>
    			<artifactId>jackson-databind</artifactId>
    			<version>2.8.8</version>
    		</dependency>
    
    		<!--JSR303数据校验支持;tomcat7及以上的服务器, tomcat7以下的服务器:el表达式。额外给服务器的lib包中替换新的标准的el -->
    		<dependency>
    			<groupId>org.hibernate</groupId>
    			<artifactId>hibernate-validator</artifactId>
    			<version>5.4.1.Final</version>
    		</dependency>
    
    		<!-- Spring-Jdbc -->
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-jdbc</artifactId>
    			<version>4.3.7.RELEASE</version>
    		</dependency>
    
    		<!--Spring-test -->
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-test</artifactId>
    			<version>4.3.7.RELEASE</version>
    		</dependency>
    
    		<!-- Spring面向切面编程 -->
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-aspects</artifactId>
    			<version>4.3.7.RELEASE</version>
    		</dependency>
    
    		<!--MyBatis -->
    		<dependency>
    			<groupId>org.mybatis</groupId>
    			<artifactId>mybatis</artifactId>
    			<version>3.4.2</version>
    		</dependency>
    		<!-- MyBatis整合Spring的适配包 -->
    		<dependency>
    			<groupId>org.mybatis</groupId>
    			<artifactId>mybatis-spring</artifactId>
    			<version>1.3.1</version>
    		</dependency>
    
    		<!-- 数据库连接池、驱动 -->
    		<dependency>
    			<groupId>com.alibaba</groupId>
    			<artifactId>druid</artifactId>
    			<version>1.1.6</version>
    		</dependency>
    		<dependency>
    			<groupId>mysql</groupId>
    			<artifactId>mysql-connector-java</artifactId>
    			<version>5.1.41</version>
    		</dependency>
    		<!-- (jstl,servlet-api,junit) -->
    		<dependency>
    			<groupId>jstl</groupId>
    			<artifactId>jstl</artifactId>
    			<version>1.2</version>
    		</dependency>
    		<dependency>
    			<groupId>javax.servlet</groupId>
    			<artifactId>javax.servlet-api</artifactId>
    			<version>3.0.1</version>
    			<scope>provided</scope>
    		</dependency>
    		<!-- junit -->
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>4.12</version>
    		</dependency>
    		
    		<dependency>
    			<groupId>log4j</groupId>
    			<artifactId>log4j</artifactId>
    			<version>1.2.17</version>
    		</dependency>
    	</dependencies>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-compiler-plugin</artifactId>
    				<version>3.2</version>
    				<configuration>
    					<source>1.8</source>
    					<target>1.8</target>
    					<encoding>UTF-8</encoding>
    				</configuration>
    			</plugin>
    			<plugin>
    				<groupId>org.apache.tomcat.maven</groupId>
    				<artifactId>tomcat7-maven-plugin</artifactId>
    				<version>2.2</version>
    				<configuration>
    					<path>/</path>
    					<port>9090</port>
    				</configuration>
    			</plugin>
    		</plugins>
    	</build>
    </project>

    c. 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="WebApp_ID" version="2.5">
    
    
    
    <!--1、启动Spring的容器  -->
    	<!-- needed for ContextLoaderListener -->
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>classpath:applicationContext.xml</param-value>
    	</context-param>
    
    	<!-- Bootstraps the root web application context before servlet initialization -->
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    	
    	<!--2、springmvc的前端控制器,拦截所有请求  -->
    	<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    	<servlet>
    		<servlet-name>dispatcherServlet</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    	<param-name>contextConfigLocation</param-name>
    			<param-value>classpath:dispatcherServletConfig.xml</param-value>
    		</init-param>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    
    	<!-- Map all requests to the DispatcherServlet for handling -->
    	<servlet-mapping>
    		<servlet-name>dispatcherServlet</servlet-name>
    		<url-pattern>/</url-pattern>
    	</servlet-mapping>
    	
    	<!-- 3、字符编码过滤器,一定要放在所有过滤器之前 -->
    	<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>
    		<init-param>
    			<param-name>forceRequestEncoding</param-name>
    			<param-value>true</param-value>
    		</init-param>
    		<init-param>
    			<param-name>forceResponseEncoding</param-name>
    			<param-value>true</param-value>
    		</init-param>
    	</filter>
    	<filter-mapping>
    		<filter-name>CharacterEncodingFilter</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
    	
    	<!-- 4、使用Rest风格的URI,将页面普通的post请求转为指定的delete或者put请求 -->
    	<filter>
    		<filter-name>HiddenHttpMethodFilter</filter-name>
    		<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    	</filter>
    	<filter-mapping>
    		<filter-name>HiddenHttpMethodFilter</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
    	<filter>
    		<filter-name>HttpPutFormContentFilter</filter-name>
    		<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
    	</filter>
    	<filter-mapping>
    		<filter-name>HttpPutFormContentFilter</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
    </web-app>

    d. 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"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	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-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">
    
    	<context:component-scan base-package="com.taotao.ssm.controller" />
    
    	<context:property-placeholder location="classpath:db.properties" />
    	<bean id="poolDataSource" class="com.alibaba.druid.pool.DruidDataSource">
    		<property name="url" value="${jdbc.url}" />
    		<property name="username" value="${jdbc.username}" />
    		<property name="password" value="${jdbc.password}" />
    		<property name="driverClassName" value="${jdbc.driver}" />
    		<property name="maxActive" value="10" />
    		<property name="minIdle" value="5" />
    	</bean>
    
    	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
    		<property name="configLocation" value="classpath:mybatisConfig.xml" />
    		<property name="mapperLocations" value="classpath:mapper/*.xml" />
    		<property name="dataSource" ref="poolDataSource" />
    	</bean>
    
    	<bean id="mapperScannerConfigurer" 	class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    		<property name="basePackage" value="com.taotao.ssm.dao" />
    	</bean>
    
    	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    		<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean" />
    		<constructor-arg name="executorType" value="BATCH" />
    	</bean>
    
    	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource" ref="poolDataSource" />
    	</bean>
    	<tx:advice id="txAdvice" transaction-manager="transactionManager">
    		<tx:attributes>
    			<tx:method name="*" />
    			<tx:method name="get*" read-only="true" />
    			<tx:method name="update*" propagation="REQUIRED" />
    			<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
    		</tx:attributes>
    	</tx:advice>
    	<aop:config>
    		<aop:advisor advice-ref="txAdvice" 	pointcut="execution(* com.taotao.ssm.service..*(..))" />
    	</aop:config>
    </beans>

    e. db.properties

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

    mysql-db 库和表.sql

     

    CREATE TABLE `tbl_dept` (
      `dept_id` int(10) NOT NULL AUTO_INCREMENT COMMENT '部门编号',
      `dept_name` varchar(255) DEFAULT NULL COMMENT '部门名',
      PRIMARY KEY (`dept_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COMMENT='部门表';
    
    CREATE TABLE `tbl_emp` (
      `emp_id` int(20) NOT NULL AUTO_INCREMENT COMMENT '员工编号',
      `emp_name` varchar(255) NOT NULL COMMENT '员工姓名',
      `gender` char(1) DEFAULT NULL COMMENT '员工性别',
      `email` varchar(255) DEFAULT NULL COMMENT '员工邮箱',
      `d_id` int(20) DEFAULT NULL COMMENT '员工所属部门编号',
      PRIMARY KEY (`emp_id`),
      KEY `fk_emp_dept` (`d_id`),
      CONSTRAINT `fk_emp_dept` FOREIGN KEY (`d_id`) REFERENCES `tbl_dept` (`dept_id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=4417 DEFAULT CHARSET=utf8 COMMENT='员工表';

    f. mybatisConfig.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="logImpl" value="STDOUT_LOGGING" />
        </settings>
    	<plugins>
    		<plugin interceptor="com.github.pagehelper.PageInterceptor">
    			<property name="reasonable" value="true"/>
    		</plugin>
    	</plugins>
    
    </configuration>

    g. dispatcherServletConfig.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.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">
    
    	<context:component-scan base-package="com.taotao.ssm.controller" />
    
    	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix" value="/WEB-INF/views" />
    		<property name="suffix" value=".jsp" />
    	</bean>
    
    	<mvc:default-servlet-handler />
    
    	<mvc:annotation-driven />
    </beans>
    

    h. log4j.properties

    log4j.rootLogger=WARN, Console
    
    #Console
    log4j.appender.Console=org.apache.log4j.ConsoleAppender
    log4j.appender.Console.layout=org.apache.log4j.PatternLayout
    #log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
    log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n%n
    #Base
    log4j.logger.org.apache.commons=ERROR
    log4j.logger.org.apache.struts=INFO
    log4j.logger.org.junit=INFO
    log4j.logger.org.displaytag=ERROR
    log4j.logger.org.springframework=INFO
    log4j.logger.org.springframework.web=INFO
    log4j.logger.org.springframework.http=INFO
    log4j.logger.org.apache.velocity=FATAL
    log4j.logger.org.apache=DEBUG
    log4j.logger.com.ibatis=DEBUG
    log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
    log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
    log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
    log4j.logger.java.sql.ResultSet=INFO
    log4j.logger.java.sql.Connection=DEBUG
    log4j.logger.java.sql.Statement=DEBUG
    log4j.logger.java.sql.PreparedStatement=DEBUG

    i.package建立


    j.最后目录结构


    k.运行命令:clean tomcat7:run


    l.我是一次性,OK。没有报错。有报错的。请留言




  • 相关阅读:
    java泛型
    枚举类与可变参数
    JAVA反射实现JdbcTemplate中查询方法 返回的结果集自动封装成对应的JAVABean对象
    JAVA反射之内省
    JAVA反射基础
    java反射实现将HashMap中的键值对封装为一个JavaBean对象
    hexo配置发布至ssh非22端口服务器
    Exception -LoggerFactory is not a Logback LoggerContext but Logback is on the classpath
    去重优化
    两个域名指向同一服务器的非80端口
  • 原文地址:https://www.cnblogs.com/ncepu/p/13694986.html
Copyright © 2020-2023  润新知