mybatis逆向工程和SSM框架整合

一、數(shù)據(jù)庫逆向工程

1、安裝mysql數(shù)據(jù)庫,創(chuàng)建數(shù)據(jù)庫,將數(shù)據(jù)庫的腳本導入數(shù)據(jù)庫管理工具。
2、使用mybatis-generator生成pojo、mapper接口及mapper映射文件。pojo是根據(jù)數(shù)據(jù)庫中的字段逆向生成的,mapper接口是java文件,mapper映射文件是xml文件。
3、將pojo復制到taotao-manager-pojo工程中,將mapper接口及映射文件復制到taotao-manager-dao工程中。
pojo工程中存有TbContent.java、TbContentExample.java
dao工程中存有TbContentMapper.java、TbContentMapper.xml
TbContent.java:序列化對象,getset。

具體步驟:
1、將generatorSqlmap工程(不是maven工程)導入workspace中
2、在工程中的generatorConfig.xml文件里設置
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ù)庫中的表對應

<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、運行generatorSqlmap工程下的GeneratorSqlmap類中的main函數(shù),run as>java application。
4、把逆向生成的com.taotao.mapper,com.taotao.pojo兩個package復制到自己的工程下。

二、SSM框架整合

  • 服務層的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ù)據(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ù)庫連接池,在功能、性能、擴展性方面,都超過其它數(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、事務管理
在taotao-manager-service工程中創(chuàng)建resource》spring》applicationContext-trans.xml,配置如下:

<!-- 事務管理器 -->
    <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的配置文件默認在: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>
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容