【SSM框架集成】jdbc.properties数据库log4j.properties日志文件 applicationContext.xml配置applicationContext-mvc.xml
程序员文章站
2022-05-23 13:05:13
...
jdbc.properties配置连接数据库四大金刚
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///mybatis
jdbc.username=root
jdbc.password=123456
logj.propertiesas日志文件配置
# ERROR错误的日志 WARN:警告 INFO:普通信息 DEBUG:调试日志 TRACE:日志
log4j.rootLogger=ERROR, stdout
#log4j.rootLogger=NONE
#把左边包名改成你自己的包名
log4j.logger.cn.itsource=TRACE
# 日志打印到控制台中
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# 日志打印的一种格式(可以灵活地指定布局模式)
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# 日志打印的格式是什么样子的 %d:日期 %p:优先级 %c:类的全名 %m:输出的结果 %n:换行
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
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:配置(xml的一个根)-->
<configuration>
<properties resource="jdbc.properties" />
<!--<typeAliases>
<package name="cn.itsource.domain" />
<package name="cn.itsource.query" />
</typeAliases>-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--引入(找到)写SQL的XML-->
<!--<mapper resource="cn/itsource/dao/ProductMapper.xml"/>-->
<!--<mapper resource="cn/itsource/_01_mapper/mapper/EmployeeMapper.xml"/>-->
<!--<mapper resource="cn/itsource/_02_many2one_result/ProductMapper.xml"/>-->
<mapper resource="cn/itsource/_03_many2one_search/ProductMapper.xml"/>
</mappers>
</configuration>
写SQL的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">
<!--namespace的命名空间和接口的全限定名一致-->
<mapper namespace="cn.itsource._01_mapper.mapper.EmployeeMapper">
<!--准备一个代码块-->
<sql id="whereSql">
<where>
<if test="name!=null and name!=''">
and name like concat("%",#{name},"%")
</if>
<if test="minAge!=null">
and age >= #{minAge}
</if>
<if test="maxAge!=null">
<![CDATA[
and age <= #{maxAge}
]]>
</if>
</where>
</sql>
<!--查询一共多少条数据-->
<select id="getCount" resultType="long" parameterType="cn.itsource._01_mapper.query.EmployeeQuery">
select count(*) from employee
<include refid="whereSql" />
</select>
<!--
条件查询的语句
CDATA:区中的数据不会被识别为语法
-->
<select id="queryAll" parameterType="cn.itsource._01_mapper.query.EmployeeQuery"
resultType="cn.itsource._01_mapper.domain.Employee">
select * from employee
<include refid="whereSql" />
</select>
<!-- 这里的id必需和方法名对应 -->
<select id="findOne" parameterType="long" resultType="cn.itsource._01_mapper.domain.Employee">
select * from employee where id= #{id}
</select>
<!--查询所有-->
<select id="findAll" resultType="cn.itsource._01_mapper.domain.Employee">
select * from employee
</select>
</mapper>
applicationContext-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"
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 http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!--1.扫描Controller-->
<context:component-scan base-package="cn.itsource.ssm.web.controller" />
<!--2.静态资源(js,css,图片...)放行-->
<mvc:default-servlet-handler />
<!--3.mvc的注解支持-->
<mvc:annotation-driven />
<!--4.视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!--5.上传解析器-->
<!-- 错误:提示告诉开发者你没有配置文件上传解析器。 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为1MB -->
<property name="maxUploadSize">
<value>10485760</value>
</property>
</bean>
<!-- 扫描easypoi的视图处理包-->
<context:component-scan base-package="cn.afterturn.easypoi.view"></context:component-scan>
<!-- 优先找easypoi的视图解析器-->
<!-- bean的视图解析器 p:order="0": 顺序在最前面 -->
<bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"
p:order="0" />
</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"
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.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--扫描service-->
<context:component-scan base-package="cn.itsource.ssm.service" />
<!--
①.jdbc.properties -> ②.DataSource -> ③.SqlSessionFactory -> ④.Mapper实现
-> ⑤.Service/TX事务 -> ⑥.集成SpringMVC
-->
<!--引入jdbc.properties-->
<context:property-placeholder location="classpath:jdbc.properties" />
<!--创建dataSource-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--
配置SqlSessionFactory -> SqlSessionFactoryBean
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--连接池-->
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:cn/itsource/ssm/mapper/*Mapper.xml" />
<!--typeAliasesPackage:这个包中所的值都会取相应的别名-->
<property name="typeAliasesPackage">
<value>
cn.itsource.ssm.domain
cn.itsource.ssm.query
</value>
</property>
</bean>
<!--
让Spring帮我们生成Mapper
mapperInterface:代表你要映射的接口类型
<bean id="employeeMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<property name="mapperInterface" value="cn.itsource.ssm.mapper.EmployeeMapper" />
</bean>
-->
<!-- 一劳永逸
Mapper(映射)Scanner(扫描)Configurer(配置)
basePackage:扫描的包(会用对象的类都创建实现)
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itsource.ssm.mapper" />
</bean>
<!--事务配置-->
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--开启事务注解的支持,默认会去找一个名称叫做transactionManager的事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>