标签:乱码 bsp 更改 sed multi classic form 表单 ibatis ndt
一、整体步骤
maven建好web工程并配置好整体包架构(三层架构)->pom文件中所需依赖导入->配置web.xml->spring-mvc配置->配置spring-mybatis配置->建立相应代码和数据表进行测试
整体结构如下
1.maven新建项目,在新建项目时加入一组东西,下载依赖会快
(
archetypeCatalog
internal
)
2.然后补充好java源包,resources配置包,三层架构包
3.配置pom.xml文件
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.yiyezhiqiu</groupId> <artifactId>ssm01</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>ssm01 Maven Webapp</name> <!-- FIXME change it to the project‘s website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--配置servlet依赖--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.0</version> <scope>provided</scope> </dependency> <!--配置spring框架依赖--> <!--配置spring-webmvc依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.0.8.RELEASE</version> </dependency> <!--引入spring-jdbc依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.0.8.RELEASE</version> </dependency> <!--配置数据库的依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!--引入数据库连接技术--> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!--配置mybatis的依赖--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <!--配置spring和mybatis整合依赖--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!--引入JSTL标签类依赖--> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--logback 日志处理--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency> <!-- https://mvnrepository.com/artifact/org.logback-extensions/logback-ext-spring --> <dependency> <groupId>org.logback-extensions</groupId> <artifactId>logback-ext-spring</artifactId> <version>0.1.4</version> </dependency> </dependencies> <build> <finalName>ssm01</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
3.配置web.xml文件(比如前端控制器DispatcherServlet)
<!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 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>Archetype Created Web Application</display-name> <!-- Spring配置 --> <!-- ================================================= --> <!--配置Spring IoC的配置文件路径 classpath相当于resources文件夹--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mybatis.xml</param-value> </context-param> <!--Log4j配置--> <!--<context-param>--> <!--<param-name>log4jRefreshInterval</param-name>--> <!--<param-value>60000</param-value>--> <!--</context-param>--> <!--logback日志配置--> <context-param> <param-name>logbackConfigLocation</param-name> <param-value>classpath:logback.xml</param-value> </context-param> <listener> <listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class> </listener> <!--配置ContextLoaderListener初始化IOC容器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 防止Spring内存溢出监听器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- Spring mvc配置 --> <!-- ================================================= --> <!--DispatcherServlet前置控制器配置--> <servlet> <!--springmvc框架默认自动找到/WEB-INF/springmvc-servlet.xml作为配置文件载入web工程中 这里手动设置位置--> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <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映射设置--> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--解决中文乱码问题的过滤器--> <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>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
4.配置spring-mvc.xml (整合spring和springmvc)
<?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-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!--配置springmvc注解扫描--> <context:component-scan base-package="com.yiyezhiqiu.controller"/> <!--配置不处理静态资源--> <mvc:default-servlet-handler/> <!--配置注解驱动--> <mvc:annotation-driven /> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!--相当于寻找/jsp/xxx.jsp文件--> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/pages/" /> <!-- 后缀 --> <property name="suffix" value=".jsp" /> </bean> <!--过滤静态资源--> <mvc:resources location="/css/" mapping="/css/**" /> <mvc:resources location="/imagess/" mapping="/images/**" /> <mvc:resources location="/js/" mapping="/js/**" /> <!--解决返回给前端中文乱码--> <mvc:annotation-driven > <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter" > <property name = "supportedMediaTypes"> <list> <value>application/json;charset=utf-8</value> <value>text/html;charset=utf-8</value> <!-- application 可以在任意 form 表单里面 enctype 属性默认找到 --> <value>application/x-www-form-urlencoded</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> </beans>
5.配置spring-mybatis(spring和mybatis整合)
5.1 、写一个jdbc.properties文件存放和mysql数据库连接的字符串信息
mysql.driver=com.mysql.jdbc.Driver mysql.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=UTF8 mysql.username=root mysql.password=root initPoolSize=3 maxPoolSizes=30
5.2、配置spirng和mybatis
<?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/cache" 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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- http://www.springframework.org/schema/mvc:这种就是spring框架的约束--> <!--使用注解开发--> <mvc:annotation-driven/> <context:component-scan base-package="com.yiyezhiqiu"/> <!--引入jdbc.properties--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--配置数据源:C3P0,主要为了和数据库进行连接--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${mysql.driver}"/> <property name="jdbcUrl" value="${mysql.url}"/> <property name="user" value="${mysql.username}"/> <property name="password" value="${mysql.password}"/> <property name="initialPoolSize" value="${initPoolSize}"/> <property name="maxPoolSize" value="${maxPoolSizes}"/> </bean> <!--配置SqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--引入数据源--> <property name="dataSource" ref="dataSource"/> <!--加载所有的映射文件--> <property name="mapperLocations" value="classpath:com.yiyezhiqiu.dao/*.xml"/> <!--tyepAliasesPackage 对应实体类的包,这个时候会自动取包名作为别名--> <property name="typeAliasesPackage" value="com.yiyezhiqiu.domain"/> <property name="configuration"> <!--可以将之前mybatis.cfg.xml的一些配置项转移到这里来--> <bean class="org.apache.ibatis.session.Configuration"> <property name="mapUnderscoreToCamelCase" value="true" /> </bean> </property> </bean> <!--自动扫描对象关系映射MapperScannerConfigurer--> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <property name="basePackage" value="com.yiyezhiqiu.dao"/> </bean> <!-- 数据库事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
6.创建代码和表
6.1、Users
package com.yiyezhiqiu.domain; import java.io.Serializable; public class Users implements Serializable { private Integer id; private Integer uid; private String userName; private String password; public Users(Integer id, Integer uid, String userName, String password) { this.id = id; this.uid = uid; this.userName = userName; this.password = password; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } 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; } @Override public String toString() { return "Users{" + "id=" + id + ", uid=" + uid + ", userName=‘" + userName + ‘\‘‘ + ", password=‘" + password + ‘\‘‘ + ‘}‘; } }
6.2、UsersController
@RestController @RequestMapping(value = "/users") public class UsersController { @Autowired IUsersService usersService; @RequestMapping(value = "/saveUsers",method = RequestMethod.GET) @ResponseBody public String saveUsers(Users users){ System.out.println("users:"+users); int result = usersService.saveUsers(users); if(result>0){ return"保存成功"; }else{ return"保存失败"; } } }
6.3、UsersDao
@Repository public interface UsersDao { int saveUsers(Users users); }
6.4、UsersDao.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.yiyezhiqiu.dao.UsersDao">
<insert id="saveUsers" parameterType="com.yiyezhiqiu.domain.Users" keyProperty="id" useGeneratedKeys="true">
insert into users(uid,userName,password) values (#{uid},#{userName},#{password})
</insert>
</mapper>
6.5、mysql表创建相应表和字段即可。
6.6、测试访问即可。
补充:加入lockback日志:
<?xml version="1.0" encoding="UTF-8"?> <!-- 从高到地低 OFF 、 FATAL 、 ERROR 、 WARN 、 INFO 、 DEBUG 、 TRACE 、 ALL --> <!-- 日志输出规则 根据当前ROOT 级别,日志输出时,级别高于root默认的级别时 会输出 --> <!-- 以下 每个配置的 filter 是过滤掉输出文件里面,会出现高级别文件,依然出现低级别的日志信息,通过filter 过滤只记录本级别的日志--> <!-- 属性描述 scan:性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true scanPeriod:设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。 debug:当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。 --> <configuration scan="true" debug="false"> <!-- 自定义定义日志文件 输入位置 tomcat 下的logs路径下 可以更改位置 --> <property name="path" value="d:/ssm_logs/ssm"/> <!--%highlight() 颜色打印 不同级别日志打印不同颜色--> <property name="CONSOLE_LOG_PATTERN" value="%highlight(%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] [line%4line] %logger{50} : %msg %n)"/> <!--info级别日志文件配置--> <appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [line%4line] %logger{50} : %msg%n</pattern> <charset>UTF-8</charset> <!-- 此处设置字符集,防止中文乱码 --> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${path}/info/info-%d{yyyy-MM-dd}.%i.log</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <!-- 最大64MB 超过最大值,会重新建一个文件--> <maxFileSize>64 MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFO</level> </filter> <!-- Safely log to the same file from multiple JVMs. Degrades performance! --> <!--如果是 true,日志会被安全的写入文件,即使其他的FileAppender也在向此文件做写入操作,效率低,默认是 false。--> <prudent>false</prudent> </appender> <!--警告文件配置--> <appender name="FILE_WARN" class="ch.qos.logback.core.rolling.RollingFileAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [line%4line] %logger{50} : %msg%n</pattern> <charset>UTF-8</charset> <!-- 此处设置字符集,防止中文乱码 --> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${path}/warn/warn-%d{yyyy-MM-dd}.%i.log</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <!-- 最大64MB 超过最大值,会重新建一个文件--> <maxFileSize>64 MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>WARN</level> </filter> <!-- Safely log to the same file from multiple JVMs. Degrades performance! --> <!--如果是 true,日志会被安全的写入文件,即使其他的FileAppender也在向此文件做写入操作,效率低,默认是 false。--> <prudent>false</prudent> </appender> <!--错误日志配置--> <appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- 文件输出的日志 的格式 --> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [line%4line] %logger{50} : %msg%n</pattern> <charset>UTF-8</charset> <!-- 此处设置字符集,防止中文乱码 --> </encoder> <!-- 配置日志所生成的目录以及生成文件名的规则 在logs/mylog-2016-10-31.0.log --> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${path}/error/error-%d{yyyy-MM-dd}.%i.log</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <!-- 最大64MB 超过最大值,会重新建一个文件--> <maxFileSize>64 MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> </filter> <!-- Safely log to the same file from multiple JVMs. Degrades performance! --> <!--如果是 true,日志会被安全的写入文件,即使其他的FileAppender也在向此文件做写入操作,效率低,默认是 false。--> <prudent>false</prudent> </appender> <!--debug配置--> <appender name="FILE_DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- 文件输出的日志 的格式 --> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [line%4line] %logger{50} : %msg%n</pattern> <charset>UTF-8</charset> <!-- 此处设置字符集,防止中文乱码 --> </encoder> <!-- 配置日志所生成的目录以及生成文件名的规则 在logs/mylog-2016-10-31.0.log --> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${path}/debug/debug-%d{yyyy-MM-dd}.%i.log</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <!-- 最大64MB 超过最大值,会重新建一个文件--> <maxFileSize>64 MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>DEBUG</level> </filter> <!-- Safely log to the same file from multiple JVMs. Degrades performance! --> <!--如果是 true,日志会被安全的写入文件,即使其他的FileAppender也在向此文件做写入操作,效率低,默认是 false。--> <prudent>false</prudent> </appender> <!--控制台输出的格式设置 --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <!-- 控制台输出的日志 的格式 --> <encoder> <pattern> ${CONSOLE_LOG_PATTERN} </pattern> <charset>UTF-8</charset> <!-- 此处设置字符集 --> </encoder> <!-- 只是DEBUG级别以上的日志才显示 --> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>DEBUG</level> </filter> </appender> <!-- root级别 (name = dao 实现SQL打印并写入日志文件,与mybatis-config.xml中的dao.对应) DEBUG --> <root name="dao" level="DEBUG"> <!-- 控制台输出 --> <appender-ref ref="STDOUT"/> <!-- 文件输出 表示开启对应的日志级别 --> <appender-ref ref="FILE_WARN"/> <appender-ref ref="FILE_INFO"/> <appender-ref ref="FILE_ERROR"/> <appender-ref ref="FILE_DEBUG"/> </root> </configuration>
总结:
1.大体创建步骤...
2.细节
2.1、Controller层,展示视图:@Controller , 展示字符串形式:@RestController
2.2、聊聊我们新添加一个配置文件,为啥就要上面一大串的约束:它的作用主要是spring加载配置文件时验证配置文件的语法正确性。
这片博客讲解:
https://blog.csdn.net/zh15732621679/article/details/79074380
标签:乱码 bsp 更改 sed multi classic form 表单 ibatis ndt
原文地址:https://www.cnblogs.com/yiyezhiqiuwuchen/p/13197649.html