整合思路
单独使用MyBatis的例子:
单击你想要生成的文件的目录之下---->点击folder即可
程序及运行结果:
xml配置:
数据库连接配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTO Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 和spring整合后这个配置将废弃 -->
<environments default="development">
<environment id="development">
<!-- 使用JDBC事务管理 -->
<transactionManager type="jdbc"></transactionManager>
<!-- 数据库连接池 -->
<dataSource type="POOLED">
<property name="driver" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost/zhouyi"/>
<property name="username" value="postgres"/>
<property name="password" value="823328"/>
</dataSource>
</environment>
</environments>
<!-- 查找sql映射文件 -->
<mappers>
<mapper resource="mapper/CustomerMapper.xml"/>
</mappers>
</configuration>
对应的数据库映射xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTO Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 该文件主要是编写mybatis中的mapper接口里面的方法,对应的sql语句 -->
<mapper namespace="com.ssm.dao.CustomerMapper">
<!-- 添加用户 并指定类型-->
<insert id="saveCustomer" parameterType="com.ssm.domain.Customer">
<!-- 提供sql语句 -->
insert into t_customer(aname,gender,telephone,address) values(#{name},#{gender},#{telephone},#{telephone})
</insert>
</mapper>
日志输出配置文件(log4j.properties)
llog4j.rootLogger=ALL,systemOut
#,logFile,logDailyFile,logRollingFile,logMail,logDB
#输出到控制台
log4j.appender.systemOut= org.apache.log4j.ConsoleAppender
log4j.appender.systemOut.layout= org.apache.log4j.PatternLayout
log4j.appender.systemOut.layout.ConversionPattern= [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n
log4j.appender.systemOut.Threshold= DEBUG
log4j.appender.systemOut.ImmediateFlush= TRUE
log4j.appender.systemOut.Target= System.out
#输出到文件
#log4j.appender.logFile= org.apache.log4j.FileAppender
#log4j.appender.logFile.layout= org.apache.log4j.PatternLayout
#log4j.appender.logFile.layout.ConversionPattern= [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n
#log4j.appender.logFile.Threshold= DEBUG
#log4j.appender.logFile.ImmediateFlush= TRUE
#log4j.appender.logFile.Append= TRUE
#log4j.appender.logFile.File= ../Struts2/WebRoot/log/File/log4j_Struts.log
#log4j.appender.logFile.Encoding= UTF-8
#按DatePattern输出到文件
#log4j.appender.logDailyFile= org.apache.log4j.DailyRollingFileAppender
#log4j.appender.logDailyFile.layout= org.apache.log4j.PatternLayout
#log4j.appender.logDailyFile.layout.ConversionPattern= [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n
#log4j.appender.logDailyFile.Threshold= DEBUG
#log4j.appender.logDailyFile.ImmediateFlush= TRUE
#log4j.appender.logDailyFile.Append= TRUE
#log4j.appender.logDailyFile.File= ../Struts2/WebRoot/log/DailyFile/log4j_Struts
#log4j.appender.logDailyFile.DatePattern= '.'yyyy-MM-dd-HH-mm'.log'
#log4j.appender.logDailyFile.Encoding= UTF-8
#设定文件大小输出到文件
#log4j.appender.logRollingFile= org.apache.log4j.RollingFileAppender
#log4j.appender.logRollingFile.layout= org.apache.log4j.PatternLayout
#log4j.appender.logRollingFile.layout.ConversionPattern= [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n
#log4j.appender.logRollingFile.Threshold= DEBUG
#log4j.appender.logRollingFile.ImmediateFlush= TRUE
#log4j.appender.logRollingFile.Append= TRUE
#log4j.appender.logRollingFile.File= ../Struts2/WebRoot/log/RollingFile/log4j_Struts.log
#log4j.appender.logRollingFile.MaxFileSize= 1MB
#log4j.appender.logRollingFile.MaxBackupIndex= 10
#log4j.appender.logRollingFile.Encoding= UTF-8
#用Email发送日志
#log4j.appender.logMail= org.apache.log4j.net.SMTPAppender
#log4j.appender.logMail.layout= org.apache.log4j.HTMLLayout
#log4j.appender.logMail.layout.LocationInfo= TRUE
#log4j.appender.logMail.layout.Title= Struts2 Mail LogFile
#log4j.appender.logMail.Threshold= DEBUG
#log4j.appender.logMail.SMTPDebug= FALSE
#log4j.appender.logMail.SMTPHost= SMTP.163.com
#log4j.appender.logMail.From= xly3000@163.com
#log4j.appender.logMail.To= xly3000@gmail.com
#log4j.appender.logMail.Cc= xly3000@gmail.com
#log4j.appender.logMail.Bcc= xly3000@gmail.com
#log4j.appender.logMail.SMTPUsername= xly3000
#log4j.appender.logMail.SMTPPassword= 1234567
#log4j.appender.logMail.Subject= Log4j Log Messages
#log4j.appender.logMail.BufferSize= 1024
#log4j.appender.logMail.SMTPAuth= TRUE
#将日志登录到MySQL数据库
#log4j.appender.logDB= org.apache.log4j.jdbc.JDBCAppender
#log4j.appender.logDB.layout= org.apache.log4j.PatternLayout
#log4j.appender.logDB.Driver= com.mysql.jdbc.Driver
#log4j.appender.logDB.URL= jdbc:mysql://127.0.0.1:3306/xly
#log4j.appender.logDB.User= root
#log4j.appender.logDB.Password= 123456
#log4j.appender.logDB.Sql= INSERT
INTOT_log4j(project_name,create_date,level,category,file_name,thread_name,line,all_category,message)values('Struts2','%d{yyyy-MM- ddHH:mm:ss}','%p','%c','%F','%t','%L','%l','%m')
mybatis整合spring
第一类:有Mapper实现类
导入jar包:mybatis-spring以及spring相关的包(整合时报配错,首先想到是不是jar包之间的不兼容)
实例(有Mapper实现类):
第二类:没有Mapper实现类(更秀,开发常用)
和有的相比,删除实现类,改为配置文件中来配置。
第三类:Spring-Mapper接口扫描(相当于第二类的优化,最佳方案,推荐使用)
将第二类中圈中的部分改为下面的即可。如果Mapper接口扫描,那么每个Mapper接口的在Spring容器中的id名称为类名:
例如:CustomerMapper–》customerMapper(默认的规则)
整合SpringMVC
导入jar包:
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SpringMvc</display-name>
<!-- 启动SpringMVC -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加一个初始化参数,来读取spring-mvc.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 启动spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 修改路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index1.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
在web.xml中配置SpringMVC编码过滤器:
<!-- 配置SpringMVC编码过滤器 -->
<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>
配置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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 扫描controller所在包 -->
<context:component-scan base-package="com.ssm.controller"></context:component-scan>
<!-- 注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 视图解析器:简化咋controller类编写的视图路径 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
编写测试: