一、目录结构图
2、配置文件内容
db.properties:
#oracle public oracle.driverClass=oracle.jdbc.driver.OracleDriver oracle.maxActive=14 oracle.initialSize=1 oracle.maxWait=120 oracle.maxIdle=14 oracle.minIdle=1 oracle.removeAbandoned=true oracle.removeAbandonedTimeout=180 oracle.timeBetweenEvictionRunsMillis=60000 oracle.minEvictableIdleTimeMillis=1800000 oracle.connectionProperties=bigStringTryClob=true;clientEncoding=UTF-8;defaultRowPrefetch=50;serverEncoding=UTF-8 #oracle DB 注:oracle和mysql只是driverClass不一样,其他配置相同 oracle.jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:ORCL oracle.jdbc.username=crh_snp oracle.jdbc.passpword=cairenhui
freemarker.properties:
#freemarker settings tag_syntax=auto_detect template_update_delay=2 default_encoding=UTF-8 output_encoding=UTF-8 locale=zh_CN date_format=yyyy-MM-dd time_format=HH:mm:ss datetime_format=yyyy-MM-dd HH:mm:ss
3、web.xml配置
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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_3_0.xsd" id="WebApp_ID" version="3.0"> <welcome-file-list> <welcome-file>page/index.html</welcome-file> <welcome-file>page/index.htm</welcome-file> <welcome-file>page/index.jsp</welcome-file> </welcome-file-list> <!-- 加载日志配置文件 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:/config/log4j.properties</param-value> </context-param> <!-- 加载配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:/datasource/dbconfig.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 对客户端请求的静态资源如图片、JS文件等的请求交由默认的servlet进行处理 ,必须写在DispatcherServlet前面,否则会被spring拦截 --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.gif</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.png</url-pattern> </servlet-mapping> <servlet> <servlet-name>config</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>config</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>config</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>config</servlet-name> <url-pattern>*.img</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>config</servlet-name> <url-pattern>*.json</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>config</servlet-name> <url-pattern>*.ws</url-pattern> </servlet-mapping> <!-- 统一工程编码过滤器 --> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <error-page> <error-code>404</error-code> <location>/404.htm</location> </error-page> <error-page> <error-code>500</error-code> <location>/500.htm</location> </error-page> </web-app>
4、dbconfig.xml(数据库连接配置)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 引入db.properties中属性 --> <!-- 方式一 :单文件引入--> <!-- <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:/config/db.properties</value> </property> </bean> --> <!-- 方式二:多文件引入 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="order" value="2" /> <property name="ignoreUnresolvablePlaceholders" value="true" /> <property name="locations"> <list> <value>classpath:config/freemarker.properties</value> <value>classpath:config/db.properties</value> <!-- 注意路径写法,上面写法要加classpath,路径前不能有/,此下面写法 --> <value>/WEB-INF/test.properties</value> </list> </property> </bean> <bean id="dataSource" name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${oracle.driverClass}" /> <property name="url" value="${oracle.jdbc.url}" /> <property name="username" value="${oracle.jdbc.username}" /> <property name="password" value="${oracle.jdbc.passpword}" /> <!-- 连接池最大使用连接数量 --> <property name="maxActive" value="${oracle.maxActive}" /> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="${oracle.maxWait}" /> <property name="poolPreparedStatements" value="true" /> <property name="defaultAutoCommit" value="true" /> <!-- 连接池最大空闲 --> <property name="maxIdle" value="${oracle.maxIdle}" /> <!-- 连接池最小空闲 --> <property name="minIdle" value="${oracle.minIdle}" /> <property name="testOnBorrow" value="true" /> <property name="testWhileIdle" value="true" /> <property name="validationQuery" value="select 1 from dual" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="${oracle.timeBetweenEvictionRunsMillis}" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="${oracle.minEvictableIdleTimeMillis}" /> </bean> <!-- 配置mybatis固定的写法 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation"> <value>classpath:mybatis/mybatis-sqlmap.xml</value> </property> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg> </bean> <!-- 配置事务管理器bean --> <!-- TransactionManager --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref local="dataSource" /> </property> </bean> <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager"> <ref bean="transactionManager"/> </property> </bean> <!-- 事务控制代理抽象定义 --> <bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> <!-- 为事务代理bean注入一个事物管理器 --> <property name="transactionManager"> <ref bean="transactionManager" /> </property> <!-- 定义事务传播属性 --> <property name="transactionAttributes"> <props> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="release*">PROPAGATION_REQUIRED</prop> <prop key="delete*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="find*">PROPAGATION_REQUIRED</prop> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean> </beans>
5、mybatis-sqlmap.xml配置
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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="cacheEnabled" value="true"/> <setting name="lazyLoadingEnabled" value="true"/> <setting name="multipleResultSetsEnabled" value="true"/> <setting name="useColumnLabel" value="true"/> <setting name="useGeneratedKeys" value="false"/> <setting name="autoMappingBehavior" value="PARTIAL"/> <setting name="defaultExecutorType" value="SIMPLE"/> <setting name="defaultStatementTimeout" value="25"/> <setting name="safeRowBoundsEnabled" value="false"/> <setting name="mapUnderscoreToCamelCase" value="false"/> <setting name="localCacheScope" value="SESSION"/> <setting name="jdbcTypeForNull" value="OTHER"/> <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/> </settings> <!-- 此配置是为bean起别名,有此配置后可在sql的xml文件中直接使用别名代替bean的全路径,如:resultType="User",所起别名不区分大小写 --> <typeAliases> <package name="com.test.model"/> </typeAliases> <mappers> <!-- sql文件配置方式一:指定文件路径位置,sql.xml文件namespace可以直接用dao名称,注意此时sql.xml文件要在resource目录下 --> <!-- <mapper resource="mybatis/sqlmap/user/user.xml"/> --> <!-- sql文件配置方式二:sql文件和dao接口放在同一个目录下,只需要配置sql.xml文件和接口所在包路径 ,但是sql。xml的namespace要是dao接口的完整路径--> <package name="com.test.web.dao"/> </mappers> </configuration>
6、brokerDao.xml文件配置(sql.xml)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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.test.web.dao.broker.BrokerDao"> <resultMap type="brokerInfo" id="brokerResultMap"> <result property="user_id" column="user_id"/> <result property="organ_flag" column="organ_flag"/> <result property="user_name" column="user_name"/> </resultMap> <select id="selectBroker" resultMap="brokerResultMap" parameterType="java.lang.String"> SELECT * FROM crh_user.brokerinfo WHERE user_id = #{user_id} </select> </mapper>
7、java代码
TestController:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Controller @RequestMapping("test") public class TestController { @Autowired TestService testService; @RequestMapping("print") public ModelAndView testController(HttpServletRequest req, HttpServletResponse resp, ModelAndView mav, String testId){ System.out.println("*****************controller come in******************"); mav.setViewName("index"); testService.testMethod(); return mav; } }
TestServiceImpl:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Service public class TestServiceImpl implements TestService { @Autowired BrokerDao brokerDao; public void testMethod() { System.out.println("***********test service method *******"); brokerDao.testBroker(); System.out.println("***********test service end*********"); } }
BrokerDaoImpl:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Repository public class BrokerDaoImpl implements BrokerDao { @Autowired private SqlSessionTemplate sqlSession; public void testBroker() { String user_id = "100020"; Brokerinfo broker = (Brokerinfo)sqlSession.selectOne("com.test.web.dao.broker.BrokerDao.selectBroker", user_id); System.out.println(broker); } }