mycpm環境搭建-1
阿新 • • 發佈:2018-12-26
1、匯入系統需要的包
負責Spring基礎的spring-webmvc,jackson(三個)
負責整合mybatis的包mybatis,mybatis-spring,mybatis-generator-core,spring-jdbc,commons-dbcp,mysql或者ojdbc
2、寫xml配置檔案,配置DispatcherServlet。配置contextConfigLocation的值為要掃描的xml檔案的地址,url為*.do這種內容。
3、配置spring-mvc檔案中的掃描包和註釋
<?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx ="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd" >
<!-- hanlderMapping -->
<mvc:annotation-driven />
<!-- 掃描Controller和Service元件物件 -->
<context:component-scan base-package="cn.wfc"/>
</beans>
4、配置spring-mybatis.xml檔案,設定mybatis的datasource,sessionfactory,和mapper掃描,配置datasource時需要先寫資料庫配置檔案
<?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<util:properties id="jdbc" location="classpath:conf/jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="#{jdbc.driver}"/>
<property name="username" value="#{jdbc.user}"/>
<property name="password" value="#{jdbc.pwd}"/>
<property name="url" value="#{jdbc.url}"/>
<property name="maxActive" value="#{jdbc.maxsize}"/>
<property name="initialSize" value="#{jdbc.initsize}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:cn/wfc/mapper/*.xml"/>
</bean>
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.wfc.dao" />
</bean>
</beans>
5、配置mybatis逆向工程內容
在硬碟中的某個位置建立generatorConfig.xml檔案。檔案中的內容為
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--指定特定資料庫的jdbc驅動jar包的位置-->
<classPathEntry location="C:\Users\Administrator.USER-20170722VE\.m2\repository\mysql\mysql-connector-java\5.1.40\mysql-connector-java-5.1.40.jar"/>
<context id="default" targetRuntime="MyBatis3">
<!-- optional,旨在建立class時,對註釋進行控制 -->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--jdbc的資料庫連線 -->
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost/cpm"
userId="root"
password="123456">
</jdbcConnection>
<!-- 非必需,型別處理器,在資料庫型別和java型別之間的轉換控制-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- Model模型生成器,用來生成含有主鍵key的類,記錄類 以及查詢Example類
targetPackage 指定生成的model生成所在的包名
targetProject 指定在該專案下所在的路徑
-->
<javaModelGenerator targetPackage="cn.wfc.entity"
targetProject="src/main/java">
<!-- 是否允許子包,即targetPackage.schemaName.tableName -->
<property name="enableSubPackages" value="false"/>
<!-- 是否對model新增 建構函式 -->
<property name="constructorBased" value="true"/>
<!-- 是否對類CHAR型別的列的資料進行trim操作 -->
<property name="trimStrings" value="true"/>
<!-- 建立的Model物件是否 不可改變 即生成的Model物件不會有 setter方法,只有構造方法 -->
<property name="immutable" value="false"/>
</javaModelGenerator>
<!--Mapper對映檔案生成所在的目錄 為每一個數據庫的表生成對應的SqlMap檔案 -->
<sqlMapGenerator targetPackage="cn.wfc.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 客戶端程式碼,生成易於使用的針對Model物件和XML配置檔案 的程式碼
type="ANNOTATEDMAPPER",生成Java Model 和基於註解的Mapper物件
type="MIXEDMAPPER",生成基於註解的Java Model 和相應的Mapper物件
type="XMLMAPPER",生成SQLMap XML檔案和獨立的Mapper介面
-->
<javaClientGenerator targetPackage="cn.wfc.dao"
targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="user" domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
6、寫反向主程式碼
public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("F:/generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
執行這個程式就可以自動生成表對應的實體類、dao和mapper檔案。