标签:4.0 ajax 监听器 reverse log4j 创建 大小 new path
eclipse 开发软件搭建简单 ssm 开源框架并连接MySQL查询
前提:准备好搭建框架的jar、连接数据库的jar、日志架包jar和tomcat
不多说 我们开始
首先 创建项目File -》New-》 Dynamic Web Project (名字随便起 我这里就起 ssm )
在src下创建基本的包
导入jar的包 将jar包放进 WebContent -》web-INF -》lib里
(?? 一大堆 不知道哪个没有用 放进去就对了)
jar包 链接:https://pan.baidu.com/s/1vr-PBZ78hkEnAoe2rPX1Hg 密码:l7u8
接着开始写配置文件
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>ssm</display-name> 4 <welcome-file-list> 5 <welcome-file>/jsp/index.jsp</welcome-file> 6 </welcome-file-list> 7 8 <context-param> 9 <param-name>contextConfigLocation</param-name> 10 <param-value>classpath:applicationContext.xml</param-value> 11 </context-param> 12 <listener> 13 <description>spring监听器</description> 14 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 15 </listener> 16 17 <filter> 18 <filter-name>characterEncodingFilter</filter-name> 19 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 20 <init-param> 21 <param-name>encoding</param-name> 22 <param-value>UTF-8</param-value> 23 </init-param> 24 <init-param> 25 <param-name>forceEncoding</param-name> 26 <param-value>true</param-value> 27 </init-param> 28 </filter> 29 30 <filter-mapping> 31 <filter-name>characterEncodingFilter</filter-name> 32 <url-pattern>/*</url-pattern> 33 </filter-mapping> 34 35 <servlet> 36 <description>spring mvc servlet</description> 37 <servlet-name>springMvc</servlet-name> 38 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 39 <init-param> 40 <description>spring mvc 配置文件</description> 41 <param-name>contextConfigLocation</param-name> 42 <param-value>classpath:springmvc.xml</param-value> 43 </init-param> 44 <init-param> 45 <param-name>activeReverseAjaxEnabled</param-name> 46 <param-value>true</param-value> 47 </init-param> 48 <load-on-startup>1</load-on-startup> 49 </servlet> 50 <servlet-mapping> 51 <servlet-name>springMvc</servlet-name> 52 <url-pattern>*.do</url-pattern> 53 </servlet-mapping> 54 55 </web-app>
applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xmlns:aop="http://www.springframework.org/schema/aop" 8 xmlns:tx="http://www.springframework.org/schema/tx" 9 xsi:schemaLocation=" 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 14 http://www.springframework.org/schema/context 15 http://www.springframework.org/schema/context/spring-context-3.1.xsd 16 http://www.springframework.org/schema/beans 17 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> 18 19 <!-- 开启注解 --> 20 <context:annotation-config /> 21 22 <!-- 自动扫描(service),(dao) --> 23 <context:component-scan base-package="service,dao"> 24 <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> 25 <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" /> 26 </context:component-scan> 27 28 <!-- properties配置文件 --> 29 <bean id="dbProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 30 <!-- 是否忽略不可解析的 --> 31 <property name="ignoreUnresolvablePlaceholders" value="true" /> 32 <property name="locations"> 33 <list> 34 <value>classpath:jdbc.properties</value> 35 </list> 36 </property> 37 </bean> 38 39 <!-- ========================================配置数据源========================================= --> 40 <!-- 配置数据源,使用的是alibaba的Druid(德鲁伊)数据源 --> 41 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 42 <property name="url" value="${url}" /> 43 <property name="username" value="${username}" /> 44 <property name="password" value="${password}" /> 45 <!-- 初始化连接大小 --> 46 <property name="initialSize" value="0" /> 47 <!-- 连接池最大使用连接数量 --> 48 <property name="maxActive" value="20" /> 49 <!-- 连接池最大空闲 --> 50 <property name="maxIdle" value="20" /> 51 <!-- 连接池最小空闲 --> 52 <property name="minIdle" value="0" /> 53 <!-- 获取连接最大等待时间 --> 54 <property name="maxWait" value="60000" /> 55 <property name="validationQuery" value="${validationQuery}" /> 56 <property name="testOnBorrow" value="false" /> 57 <property name="testOnReturn" value="false" /> 58 <property name="testWhileIdle" value="true" /> 59 <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> 60 <property name="timeBetweenEvictionRunsMillis" value="60000" /> 61 <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> 62 <property name="minEvictableIdleTimeMillis" value="25200000" /> 63 <!-- 打开removeAbandoned功能 --> 64 <property name="removeAbandoned" value="true" /> 65 <!-- 1800秒,也就是30分钟 --> 66 <property name="removeAbandonedTimeout" value="1800" /> 67 <!-- 关闭abanded连接时输出错误日志 --> 68 <property name="logAbandoned" value="true" /> 69 <!-- 监控数据库 --> 70 <!-- <property name="filters" value="stat" /> --> 71 <property name="filters" value="mergeStat" /> 72 </bean> 73 <!-- ========================================针对myBatis的配置项============================== --> 74 <!-- 配置sqlSessionFactory --> 75 76 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 77 <!-- 实例化sqlSessionFactory时需要使用上述配置好的数据源以及SQL映射文件 --> 78 <property name="dataSource" ref="dataSource" /> 79 <property name="configLocation" value="classpath:mybatis.cofig.xml"/> 80 <!-- 自动扫描Mybatis的Mapper.xml文件 --> 81 <property name="mapperLocations" value="classpath:/mapper/*.xml" /> 82 </bean> 83 84 <!-- 配置扫描器 --> 85 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 86 <!-- 扫描com.dao这个包以及它的子包下的所有映射接口类 --> 87 <property name="basePackage" value="dao" /> 88 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 89 </bean> 90 91 <!-- 配置事务管理器 --> 92 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 93 <property name="dataSource" ref="dataSource" /> 94 </bean> 95 96 <!-- 拦截器方式配置事物 --> 97 <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> 98 <tx:attributes> 99 <tx:method name="add*" propagation="REQUIRED" /> 100 <tx:method name="append*" propagation="REQUIRED" /> 101 <tx:method name="insert*" propagation="REQUIRED" /> 102 <tx:method name="save*" propagation="REQUIRED" /> 103 <tx:method name="update*" propagation="REQUIRED" /> 104 <tx:method name="modify*" propagation="REQUIRED" /> 105 <tx:method name="edit*" propagation="REQUIRED" /> 106 <tx:method name="delete*" propagation="REQUIRED" /> 107 <tx:method name="remove*" propagation="REQUIRED" /> 108 <tx:method name="repair" propagation="REQUIRED" /> 109 <tx:method name="delAndRepair" propagation="REQUIRED" /> 110 <tx:method name="get*" propagation="SUPPORTS" /> 111 <tx:method name="find*" propagation="SUPPORTS" /> 112 <tx:method name="load*" propagation="SUPPORTS" /> 113 <tx:method name="search*" propagation="SUPPORTS" /> 114 <tx:method name="datagrid*" propagation="SUPPORTS" /> 115 <tx:method name="*" propagation="SUPPORTS" /> 116 </tx:attributes> 117 </tx:advice> 118 119 <aop:config> 120 <!-- 配置所有的Advice,使用正则表达式,配置所有类,所有返回值的方法 --> 121 <aop:pointcut id="transactionPointcut" expression="execution(* com.service..*Impl.*(..))" /> 122 <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /> 123 </aop:config> 124 125 <!-- 配置druid监控spring jdbc --> 126 <bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"></bean> 127 <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype"> 128 <property name="patterns"> 129 <list> 130 <value>service.*</value> 131 </list> 132 </property> 133 </bean> 134 <aop:config> 135 <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" /> 136 </aop:config> 137 138 </beans>
jdbc.properties
1 driverClassName=com.mysql.jdbc.Driver 2 url=jdbc\:mysql\://localhost\:3306/mydata 3 username=root 4 password=12138
log4j.properties
1 log4j.rootCategory=INFO,stdout,logfile 2 3 log4j.rootLogger=info,stdout 4 5 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 6 log4j.appender.stdout.Target=System.out 7 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 8 log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %c{1}:%L %5p - %m%n 9 10 log4j.logger.com.netqin=INFO 11 log4j.logger.org.springframework=INFO 12 13 14 log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender 15 log4j.appender.logfile.DatePattern=‘.‘yyyy-MM-dd 16 log4j.appender.logfile.File=D:/tomcat.log 17 log4j.appender.logfile.layout=org.apache.log4j.PatternLayout 18 log4j.appender.logfile.layout.ConversionPattern=%d %p [%X{userName}][%X{userLocale}][%c] - <%m>%n
mybatis.cofig.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <settings> 7 <!-- 打印查询语句 --> 8 <setting name="logImpl" value="STDOUT_LOGGING" /> 9 </settings> 10 </configuration>
springmvc.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:mvc="http://www.springframework.org/schema/mvc" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-3.1.xsd 11 http://www.springframework.org/schema/mvc 12 http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> 13 14 <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 --> 15 <context:component-scan base-package="web"> 16 <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 17 </context:component-scan> 18 19 <!-- 日期转换 必须放在<mvc:annotation-driven />前面 --> 20 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean> 21 22 <!-- 开启注解 --> 23 <context:annotation-config /> 24 25 <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> 26 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> 27 28 <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> 29 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" > 30 <property name="prefix" value="/"></property> 31 <!-- <property name="prefix" value="/WEB-INF/jsp"></property> --> 32 <property name="suffix" value=".jsp"></property> 33 </bean> 34 35 </beans>
到这里配置文件就差不多了(配置文件的内容确实有点多 我也没办法 )
接着就开始建类文件
现在开始生成类和表mapper(如果生成不了 我们也可以手动敲)
Userlist.java
1 package po; 2 // Generated 2018-4-24 20:05:07 by Hibernate Tools 3.5.0.Final 3 4 /** 5 * Userlist generated by hbm2java 6 */ 7 public class Userlist implements java.io.Serializable { 8 9 /** 10 * 11 */ 12 private static final long serialVersionUID = 1L; 13 14 15 private int user_id; 16 private String user_name; 17 private String user_password; 18 private String user_sex; 19 private Integer user_age; 20 21 public int getUser_id() { 22 return user_id; 23 } 24 public void setUser_id(int user_id) { 25 this.user_id = user_id; 26 } 27 public String getUser_name() { 28 return user_name; 29 } 30 public void setUser_name(String user_name) { 31 this.user_name = user_name; 32 } 33 public String getUser_password() { 34 return user_password; 35 } 36 public void setUser_password(String user_password) { 37 this.user_password = user_password; 38 } 39 public String getUser_sex() { 40 return user_sex; 41 } 42 public void setUser_sex(String user_sex) { 43 this.user_sex = user_sex; 44 } 45 public Integer getUser_age() { 46 return user_age; 47 } 48 public void setUser_age(Integer user_age) { 49 this.user_age = user_age; 50 } 51 @Override 52 public String toString() { 53 return "Userlist [user_id=" + user_id + ", user_name=" + user_name + ", user_password=" + user_password 54 + ", user_sex=" + user_sex + ", user_age=" + user_age + "]"; 55 } 56 57 }
UserlistMapper.java
1 package dao; 2 3 import java.util.List; 4 5 import po.Userlist; 6 7 public interface UserlistMapper { 8 int insert(Userlist record); 9 10 List<Userlist> findAll(); 11 }
UserlistMapper.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper namespace="dao.UserlistMapper" > 4 <resultMap id="BaseResultMap" type="po.Userlist" > 5 <result column="user_id" property="user_id" jdbcType="INTEGER" /> 6 <result column="user_name" property="user_name" jdbcType="VARCHAR" /> 7 <result column="user_password" property="user_password" jdbcType="VARCHAR" /> 8 <result column="user_sex" property="user_sex" jdbcType="VARCHAR" /> 9 <result column="user_age" property="user_age" jdbcType="INTEGER" /> 10 </resultMap> 11 <insert id="insert" parameterType="po.Userlist" > 12 insert into userlist (user_id, user_name, user_password, 13 user_sex, user_age) 14 values (#{user_id,jdbcType=INTEGER}, #{user_name,jdbcType=VARCHAR}, #{user_password,jdbcType=VARCHAR}, 15 #{user_sex,jdbcType=VARCHAR}, #{user_age,jdbcType=INTEGER}) 16 </insert> 17 <select id="findAll" resultType="po.Userlist">select * from userlist</select> 18 <!-- 修改,删除也是类似这样的打 --> 19 </mapper>
到这一步后就差不多完成 接着就是service 和 action(我这是web)
IUserService.java
1 package service; 2 3 import java.util.List; 4 5 import po.Userlist; 6 7 public interface IUserService { 8 9 List<Userlist> findAll(); 10 }
UserServiceImpl.java
1 package service.impl; 2 3 import java.util.List; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 import org.springframework.transaction.annotation.Transactional; 8 9 import dao.UserlistMapper; 10 import po.Userlist; 11 import service.IUserService; 12 13 @Transactional 14 @Service 15 public class UserServiceImpl implements IUserService{ 16 17 @Autowired 18 private UserlistMapper usermapper; 19 20 @Override 21 public List<Userlist> findAll() { 22 // TODO Auto-generated method stub 23 return usermapper.findAll(); 24 25 } 26 }
userWeb.java
1 package web; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.util.List; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 import org.springframework.beans.factory.annotation.Autowired; 12 import org.springframework.stereotype.Controller; 13 import org.springframework.web.bind.annotation.RequestMapping; 14 15 import net.sf.json.JSONObject; 16 import po.Userlist; 17 import service.IUserService; 18 19 @Controller 20 @RequestMapping("/userweb") 21 public class userWeb { 22 23 @Autowired 24 private IUserService iUserService; 25 26 @RequestMapping("/touser") 27 public void toUser(HttpServletRequest request, HttpServletResponse response) 28 throws ServletException, IOException{ 29 response.setCharacterEncoding("UTF-8"); 30 31 List<Userlist> list=iUserService.findAll(); 32 JSONObject jsonobj=new JSONObject(); 33 PrintWriter write=response.getWriter(); 34 if(list.size()>0){// 35 jsonobj.put("data",list); 36 }else{ 37 } 38 write.print(jsonobj); 39 write.flush(); 40 write.close(); 41 } 42 43 }
index.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 4 <% 5 String path = request.getContextPath(); 6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 7 %> 8 9 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 10 <html> 11 <head> 12 13 <base href="<%=basePath%>"> 14 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 15 <title>Insert title here</title> 16 </head> 17 <body> 18 <table align="center" > 19 <thead> 20 <tr> 21 <td>用户名</td> 22 <td>密码</td> 23 <td>性别</td> 24 <td>年龄</td> 25 <td width="5%" class="td_title">操作</td> 26 <td width="5%" class="td_title">操作</td> 27 <td align="center"><a href=‘${ctx}/user/LoginAction!connect.action?type=toadd‘>新增</a></td> 28 </tr> 29 </thead> 30 <tbody></tbody> 31 </table> 32 33 <script src="js/jquery-1.11.1.min.js"></script> 34 <script type="text/javascript"> 35 !function ($) {data();}(window.jQuery);//开始 36 37 function data(){ 38 $.post("${ctx}/userweb/touser.do", function (data) { 39 if(data.data!=null&&data.data.length>0){ 40 var json=data.data; 41 var hhtml=""; 42 for ( var i = 0,j = json.length;i<j; i++) { 43 hhtml+="<tr>"+ 44 "<td title="+json[i].user_name+">"+json[i].user_name+"</td>"+ 45 "<td title="+json[i].user_password+">"+json[i].user_password+"</td>"+ 46 "<td title="+json[i].user_sex+">"+json[i].user_sex+"</td>"+ 47 "<td title="+json[i].user_age+">"+json[i].user_age+"</td>"+ 48 "<td align=‘center‘ > <a>修改</a></td>"+ 49 "<td align=‘center‘ > <a>删除</a></td>"+"</tr>"; 50 } 51 $("table tbody").html(hhtml); 52 } 53 },"json"); 54 } 55 </script> 56 </body> 57 </html>
到这可以说完成了简单的eclipse搭建ssm框架 并连接MySQL查询
不多说了!看看结果吧
( ps:一定要记得写 spring 的注入)
希望可以帮到你!!!
eclipse 搭建ssm (Spring+SpringMVC+MyBatis)
标签:4.0 ajax 监听器 reverse log4j 创建 大小 new path
原文地址:https://www.cnblogs.com/dwxxem/p/8947528.html