【準備工作】
1,以 scott模式登錄 Oracle數據庫,並新建表t_ent_ent_base,SQL腳
本,並新增測試數據5條以上
create table t_ent_ent_base (ent_code CHAR(32) DEFAULT sys_guid() not null,-- 企業編號
ent_name VARCHAR2(150),-- 企業名稱c
logo VARCHAR2(500),--企業 logo
loc VARCHAR2(50),--企業所在地
addr VARCHAR2(100),--企業詳細地址
zip_code CHAR(6),-- 郵政編碼
biz_lic VARCHAR2(50) not null,--營業執照編號
biz_lic_auth VARCHAR2(100),--營業執照發證機關
ent_type VARCHAR2(50),--企業類型(股份,國企,央企)
fnd_date DATE,-- 成立日期
corp VARCHAR2(50),--法人
biz_scope VARCHAR2(200),-- 經營範圍
contact VARCHAR2(50),-- 聯繫人
tel VARCHAR2(50),-- 聯繫電話
ofc_tel VARCHAR2(50),-- 辦公電話
ent_desc VARCHAR2(1000),-- 企業簡介
constraint PK_T_ENT_ENT_BASE primary key (ent_code)
);
步驟 2,參照新建Web項目 ent,添加 Spring、Spring MVC、MyBatis、Oracle JDBC驅動、Druid數據源等庫文件。
步驟 3,修改部署描述符 web.xml文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>DruidStatView</servlet-name>
<servlet-class>
com.alibaba.druid.support.http.StatViewServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DruidStatView</servlet-name>
<url-pattern>/druid/*</url-pattern>
</servlet-mapping>
步驟 4,在 WEB-INF目錄中,新建 mvc-servlet.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<context:component-scan base-package="com.it.ent.ctrl"/>
<mvc:resources location="/WEB-INF/res/js/*" mapping="/js/**"/>
<mvc:resources location="/WEB-INF/res/css/*" mapping="/css/**"/>
<mvc:resources location="/WEB-INF/res/img/*" mapping="/img/**"/>
<mvc:annotation-driven/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
步驟 5,在工程的 src目錄下,新建spring-base.xml文件
<!--讀取屬性文件 -->
<bean id="config"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean>
<!--配置 druid數據源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!--基本屬性 url、user、password -->
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!--配置連接池初始化大小、最小、最大 -->
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<property name="maxActive" value="20" />
<!--配置獲取連接等待超時的時間 -->
<property name="maxWait" value="60000" />
<!--配置間隔多久才進行一次檢測,檢測需要關閉的空閒連接,單位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!--配置一個連接在池中最小生存的時間,單位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 'x' FROM DUAL" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!--打開 PSCache,並且指定每個連接上 PSCache的大小 -->
<property name="poolPreparedStatements" value="false" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
<!--配置監控統計攔截的 filters -->
<property name="filters" value="stat" />
</bean>
<!--掃描 service,自動生成 Bean-->
<context:component-scan base-package="com.it.ent.service" />
<!--配置 mybatis會話工廠 -->
<bean id="sessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation"
value="classpath:mybatis-config.xml"></property>
</bean>
<!--自動掃描 mybatis映射接口,並生成實現類 -->
<mybatis:scan base-package="com.it.ent.dao" />
<!--配置事務管理器 -->
<bean id="txMgr" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置聲明試事務 -->
<tx:advice transaction-manager="txMgr" id="txAdvice">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="search*" propagation="REQUIRED" read-only="true" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* *..service..*.*(..))"
id="txSrvMethod" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txSrvMethod" />
</aop:config>
步驟 6,在工程 src目錄下,新建 jdbc.properties屬性文件,用於配置數據庫連接信息
driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
username=scott
password=tiger
步驟 7,在工程 src目錄下,新建 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>
<!--配置日誌系統,採用 log4j作爲日誌工具 -->
<settings>
<setting name="logImpl" value="LOG4J" />
</settings>
<!--配置別名 -->
<typeAliases>
<package name="com.it.ent.entity" />
</typeAliases>
</configuration>
步驟 8,新建 com.it.ent.entity包,並創建 Enterprise實體類
public class Enterprise implements Serializable{
private static final long serialVersionUID = -8494899139939004344L;
private String ent_code;
private String ent_name;
private String logo;
private String loc;
private String addr;
/*省略 getter/setter方法和構造方法*/
}
步驟 9,新建 com.it.ent.dao包,並創建 IEnterpriseDao數據訪問接口
public interface IEnterpriseDao {
List<Enterprise> searchEntList(Integer begin,Integer end);
}
步驟 10,在 com.it.ent.dao包中,新建 IEnterpriseDao.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.it.ent.dao.IEnterpriseDao">
<!--企業列表數據分頁查詢語句,注意帶排序的分頁查詢寫法-->
<select id="searchEntList" resultType="enterprise">
SELECT * FROM
(SELECT t.*, ROWNUM rn FROM
(SELECT ent_name,biz_lic,fnd_date
FROM t_ent_ent_base t ORDER BY fnd_date) t
WHERE ROWNUM <=#{1})
WHERE rn > #{0}
</select>
</mapper>
步驟 11,新建 com.it.ent.service 包,並在包中新建 IEnterpriseService 業務接口
public interface IEnterpriseService {
List<Enterprise> searchEntList(Integer begin,Integer end);
}
步驟 12,在 com.it.ento.service 包中,新建 EnterpriseServiceImpl 業務實現類
@Service
public class EnterpriseServiceImpl implements IEnterpriseService{
@Autowired
private IEnterpriseDao enterpriseDao;
public void setEnterpriseDao(IEnterpriseDao enterpriseDao) {
this.enterpriseDao = enterpriseDao;
}
public List<Enterprise> searchEntList(Integer begin, Integer end) {
return enterpriseDao.searchEntList(begin, end);
}
}
步驟 13,新建 com.it.ent.ctrl包,並在包中新建 EnterpriseController 請求處理類
@Controller
public class EnterpriseController {
@Autowired
private IEnterpriseService enterpriseService;
public void setEnterpriseService(IEnterpriseService enterpriseService) {
this.enterpriseService = enterpriseService;
}
/**
*請求處理方法,訪問 url:http://localhost:8080/ent/list/?pageNo=2
* @param pageNo所需也頁碼
* @return模型數據和邏輯視圖
*/
@RequestMapping({"/list"})
public ModelAndView list(Integer pageNo){
Integer begin = (pageNo - 1) * Constants.PAGE_SIZE;
Integer end = begin + Constants.PAGE_SIZE;
ModelAndView modelAndView = new ModelAndView(ViewNames.ENT_LIST);
List<Enterprise> list = enterpriseService.searchEntList(begin, end);
modelAndView.addObject("entList", list);
return modelAndView;
}
}
步驟 14,在 WEB-INF/pages工程文件夾下新建 ent_list.jsp文件,展現企業列
表數據。(內容省略……)
步驟 15,發佈應用,並在瀏覽器地址欄輸入http://localhost:8080/ent/list?pageNo=2
進行測試。
ok完成工程