一醉箕、數(shù)據(jù)庫逆向工程
1钾腺、安裝mysql數(shù)據(jù)庫,創(chuàng)建數(shù)據(jù)庫讥裤,將數(shù)據(jù)庫的腳本導(dǎo)入數(shù)據(jù)庫管理工具放棒。
2、使用mybatis-generator生成pojo己英、mapper接口及mapper映射文件间螟。pojo是根據(jù)數(shù)據(jù)庫中的字段逆向生成的,mapper接口是java文件损肛,mapper映射文件是xml文件厢破。
3、將pojo復(fù)制到taotao-manager-pojo工程中治拿,將mapper接口及映射文件復(fù)制到taotao-manager-dao工程中摩泪。
pojo工程中存有TbContent.java、TbContentExample.java
dao工程中存有TbContentMapper.java忍啤、TbContentMapper.xml
TbContent.java:序列化對象加勤,getset。
具體步驟:
1同波、將generatorSqlmap工程(不是maven工程)導(dǎo)入workspace中
2鳄梅、在工程中的generatorConfig.xml文件里設(shè)置
2.1配置連接數(shù)據(jù)庫信息:驅(qū)動類、連接地址未檩、用戶名戴尸、密碼
<jdbcConnection driveClass="com.mysql.jdbc.Driver" connectionUrl="jdbc:mysql://localhost:3306/taotao" userId="root" password="root">
</jdbcConnection>
2.2配置pojo、mapper指定路徑
<!--targetProject:生成pojo的位置-->
<javaModelGenerator targetPackage="com.taotao.pojo" targetProject=".\src">
...
</javaModelGenerator>
<!--targetProject:生成mapper映射文件的位置-->
<sqlMapGenerator targetPackage="com.taotao.mapper" targetProject=".\src">
...
</sqlMapGenerator>
<!--targetProject:生成mapper接口的位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.taotao.mapper" targetProject=".\src">
...
</javaClientGenerator>
2.3指定數(shù)據(jù)庫表冤狡,與數(shù)據(jù)庫中的表對應(yīng)
<table schema="" tableName="tb_content"></table>
<table schema="" tableName="tb_content_category"></table>
<table schema="" tableName="tb_item"></table>
<table schema="" tableName="tb_item_cat"></table>
<table schema="" tableName="tb_item_desc"></table>
<table schema="" tableName="tb_item_param"></table>
<table schema="" tableName="tb_item_param_item"></table>
<table schema="" tableName="tb_order"></table>
<table schema="" tableName="tb_order_item"></table>
<table schema="" tableName="tb_order_shipping"></table>
<table schema="" tableName="tb_user"></table>
3孙蒙、運行g(shù)eneratorSqlmap工程下的GeneratorSqlmap類中的main函數(shù)项棠,run as>java application。
4挎峦、把逆向生成的com.taotao.mapper香追,com.taotao.pojo兩個package復(fù)制到自己的工程下。
二坦胶、SSM框架整合
- 服務(wù)層的spring-dao透典、spring-service、spring-trans等(父)容器由web.xml中的ContextLoaderListener初始化顿苇。
- 表現(xiàn)層的springmvc(子)容器由web.xml中的DispatcherServlet初始化峭咒。
父容器不能訪問子容器對象,子容器可以訪問父容器對象纪岁。
1凑队、Dao整合
1.1、創(chuàng)建SqlMapConfig.xml配置文件
在taotao-manager-service工程中創(chuàng)建resource》mybatis》SqlMapConfig.xml幔翰,配置如下:
<configuration>
<!-- 配置分頁插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 配置數(shù)據(jù)庫的方言 -->
<!-- 設(shè)置數(shù)據(jù)庫類型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數(shù)據(jù)庫-->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
1.2漩氨、Spring整合mybatis,創(chuàng)建applicationContext-dao.xml
在taotao-manager-service工程中創(chuàng)建resource》spring》applicationContext-dao.xml导匣,
所有service實現(xiàn)類都放在spring容器中管理才菠,由spring配置數(shù)據(jù)庫連接池,管理SqlSessionFactory贡定、Mapper代理對象赋访,
<!-- 配置數(shù)據(jù)庫連接池 -->
<!-- 加載配置文件 -->
<context:property-placeholder location="classpath:properties/*.properties" />
<!-- 數(shù)據(jù)庫連接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="driverClassName" value="${jdbc.driver}" />
<property name="maxActive" value="10" />
<property name="minIdle" value="5" />
</bean>
<!-- SqlSessionFactory -->
<!-- 讓spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 數(shù)據(jù)庫連接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加載mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
</bean>
<!-- Mapper映射文件的包掃描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.taotao.mapper" />
</bean>
resource》properties》db.properties中如下:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.25.134:3306/taotao?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
Druid是目前最好的數(shù)據(jù)庫連接池,在功能缓待、性能蚓耽、擴(kuò)展性方面,都超過其它數(shù)據(jù)庫連接池旋炒,包括DBCP步悠、C3P0、BoneCP瘫镇、Proxool鼎兽、JBoss DataSource.
2、Service整合
2.1铣除、管理service
在taotao-manager-service工程中創(chuàng)建resource》spring》applicationContext-service.xml谚咬,配置如下:
<!-- 配置包掃描器,掃描所有帶@Service注解的類 -->
<context:component-scan base-package="com.taotao.service"/>
2.2尚粘、事務(wù)管理
在taotao-manager-service工程中創(chuàng)建resource》spring》applicationContext-trans.xml择卦,配置如下:
<!-- 事務(wù)管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 數(shù)據(jù)源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 傳播行為 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.taotao.service.*.*(..))" />
</aop:config>
2.3、web.xml管理
在taotao-manager-service工程中創(chuàng)建resources》webapp》WEB-INF》web.xml,配置如下:
<display-name>taotao-manager</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 初始化spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3秉继、表現(xiàn)層整合
3.1祈噪、sprigmvc.xml
在taotao-manager-web工程中創(chuàng)建resources》spring》springmvc.xml,配置如下:
<!-- 加載屬性文件 -->
<context:property-placeholder location="classpath:resource/resource.properties"/>
<!-- 配置注解驅(qū)動 -->
<mvc:annotation-driven />
<!-- 視圖解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
3.2尚辑、web.xml
在taotao-manager-web工程中創(chuàng)建resources》webapp》WEB-INF》web.xml辑鲤,配置如下:
<display-name>taotao-manager-web</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- post亂碼過濾器 -->
<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>
<!-- 前端控制器 -->
<servlet>
<servlet-name>taotao-manager-web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation不是必須的, 如果不配置contextConfigLocation腌巾, springmvc的配置文件默認(rèn)在:WEB-INF/servlet的name+"-servlet.xml" -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>taotao-manager-web</servlet-name>
<!-- 攔截所有請求jsp除外 -->
<url-pattern>/</url-pattern>
</servlet-mapping>