一、目錄結構圖
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配置
<?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(資料庫連線配置)
<?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配置
<?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)
<?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:
@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:
@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:
@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); } }