本文爲學習筆記,直接複製粘貼了網上大量資源,參考內容如下給出地址連接,侵刪。css
參考博客:html
解決idea新建maven項目時一直loading問題mysql
maven中GroupID 和ArtifactID怎麼寫git
Spring MVC welcome-file-list 問題github
Mybatis-mybatis自動生成代碼提示"Cannot obtain primary key information from ..."解決方案web
使用Mybatis-Generator自動生成Dao、Model、Mapping相關文件(轉)redis
file->new->new project,選擇maven。spring
這裏可能會遇到archetype一直加載的問題。緣由:idea一直讀本身的配置裏緩存致使的。sql
打開:Setting---->Build Tools → Maven → Importing
VM options for importer 的值改成 -Xmx1024m。
C:\Users\Administrator.IntelliJIdea2016.1\system\Maven(或者C:\Users\hc001.IntelliJIdea2016.1\system\Maven,總之在C盤找到.IntelliJIdea2016.1緩存) ,將Maven文件裏面清空,而後重啓idea。(這種方法我沒有測試)
解決以後,勾選crete from archetype,並選擇如圖所示的archetype:
選擇next,填寫GroupId和ArtifactId,GroupID 是項目組織惟一的標識符,實際對應JAVA的包的結構,是main目錄裏java的目錄結構。 好比源文件java包下java包結構是com.mycompanyname.mygroupname,mygroupname下一級目錄爲項目名,那麼GroupId就填com.mycompanyname.mygroupname,而ArtifactId通常填寫項目名。
填寫project name 和 project location,點擊finish。
建完項目後,idea會自動導入基本的項目結構,這時注意idea右下角彈窗提示,容許idea自動導入。導入後的項目基本結構如圖:
我通常使用的目錄結構如圖,文件詳細內容和配置代碼後面會給出。建立src/main下面的java文件後,選中java,右鍵mark directory as ,選擇java root。
配置文件中注意註釋,有的註釋指出了應該修改爲你本身的一些東西。
首先把引入jar包,因爲項目是經過maven管理的,因此只要添加pom文件中的dependency。
<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- $Id: pom.xml 642118 2008-03-28 08:04:16Z reinhard $ --> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <packaging>war</packaging> <name>discuss</name> <groupId>cn.edu.jlu</groupId> <artifactId>disscus</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.7</version> <configuration> <connectors> <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> <port>8888</port> <maxIdleTime>30000</maxIdleTime> </connector> </connectors> <webAppSourceDirectory>${project.build.directory}/${pom.artifactId}-${pom.version} </webAppSourceDirectory> <contextPath>/</contextPath> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.9.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.9.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.2.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.8.5</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> </dependencies> </project>
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--自定義攔截器--> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/*.do"/> <!--須要對應本身的攔截器的地址--> <bean class="discuss.interceptor.MyInterceptor"></bean> </mvc:interceptor> </mvc:interceptors> </beans>
<?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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="discuss.*"/> <!-- 開啓SpringMVC註解模式 --> <mvc:annotation-driven/> <!-- 靜態資源默認servlet配置 --> <mvc:default-servlet-handler/> <bean name="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- 啓動SpringMVC的註解功能,完成請求和註解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter"/> <!-- JSON轉換器 --> </list> </property> </bean> <!-- 定義跳轉的文件的先後綴 ,視圖模式配置 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="order" value="1"/> <property name="prefix" value="/jsp/" /> <property name="suffix" value=".jsp"/> </bean> <!--這裏是對靜態資源的映射--> <mvc:resources mapping="/js/**" location="/js/" /> <mvc:resources mapping="/css/**" location="/css/" /> <mvc:resources mapping="/img/**" location="/img/" /> <!-- 文件上傳配置 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 默認編碼 --> <property name="defaultEncoding" value="UTF-8"/> <!-- 上傳文件大小限制爲31M,31*1024*1024 --> <property name="maxUploadSize" value="32505856"/> <!-- 內存中的最大值 --> <property name="maxInMemorySize" value="4096"/> </bean> </beans>
<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="discuss.service" use-default-filters="false"/> <!-- 配置數據庫相關參數properties的屬性:${url} --> <context:property-placeholder location="classpath:jdbc/jdbc.properties"/> <!-- 數據庫鏈接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="maxPoolSize" value="${c3p0.maxPoolSize}"/> <property name="minPoolSize" value="${c3p0.minPoolSize}"/> <property name="autoCommitOnClose" value="${c3p0.autoCommitOnClose}"/> <property name="checkoutTimeout" value="${c3p0.checkoutTimeout}"/> <property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}"/> </bean> <!-- 配置SqlSessionFactory對象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入數據庫鏈接池 --> <property name="dataSource" ref="dataSource"/> <!-- 掃描model包 使用別名 --> <property name="typeAliasesPackage" value="discuss.entity"/> <!-- 掃描sql配置文件:mapper須要的xml文件 --> <property name="mapperLocations" value="classpath:mapper/*.xml"/> <property name="configurationProperties"> <props> <prop key="mapUnderscoreToCamelCase">true</prop> </props> </property> </bean> <!-- 配置掃描Dao接口包,動態實現Dao接口,注入到spring容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- 給出須要掃描Dao接口包 --> <property name="basePackage" value="discuss.dao"/> </bean> <!-- 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入數據庫鏈接池 --> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置基於註解的聲明式事務 --> <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> </beans>
url,username,password需手動修改爲本身的數據庫
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/discuss?useUnicode=true&characterEncoding=utf-8 jdbc.username=root jdbc.password=root #最大鏈接數 c3p0.maxPoolSize=30000 #最小鏈接數 c3p0.minPoolSize=10 #關閉鏈接後不自動commit c3p0.autoCommitOnClose=false #獲取鏈接超時時間 c3p0.checkoutTimeout=10000 #當獲取鏈接失敗重試次數 c3p0.acquireRetryAttempts=2
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!--首頁,這裏並無實現,實現請看參考連接--> <welcome-file-list> <welcome-file>jsp/index.jsp</welcome-file> </welcome-file-list> <!--springMVC攔截器--> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext*.xml</param-value> <!-- <param-value>classpath:bean/applicationContext.xml</param-value>--> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <!--springMVC攔截的地址格式--> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <context-param> <!--springmvc的配置文件--> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext*.xml</param-value> </context-param> <!-- 編碼過濾器 --> <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> <!-- spring監聽器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 防止spring內存溢出監聽器,好比quartz --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <session-config> <session-timeout>30</session-timeout> </session-config> </web-app>
測試用sql腳本,建立測試用的user表和插入一條數據。
CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用戶ID', `email` varchar(255) NOT NULL COMMENT '用戶郵箱', `password` varchar(255) NOT NULL COMMENT '用戶密碼', `username` varchar(255) NOT NULL COMMENT '用戶暱稱', `role` varchar(255) NOT NULL COMMENT '用戶身份', `status` int(1) NOT NULL COMMENT '用戶狀態', `regTime` datetime NOT NULL COMMENT '註冊時間', `regIp` varchar(255) NOT NULL COMMENT '註冊IP', PRIMARY KEY (`id`), UNIQUE KEY `email` (`email`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; INSERT INTO `user` VALUES ('1', 'xxx', 'xxxxx', 'xxxxx', 'root', '0', '2017-03-28 09:40:31', '127.0.0.1'); SET FOREIGN_KEY_CHECKS=1;
在mapper目錄下建立測試用的user.xml,創建數據庫表和實體user類的映射關係。
<?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"> <!-- 設置爲IUserDao接口方法提供sql語句配置 --> <mapper namespace="discuss.dao.IUserDao"> <resultMap id="userList" type="discuss.entity.User"> <id column="id" property="id"/> <id column="email" property="email"/> <id column="password" property="password"/> <id column="username" property="username"/> <id column="role" property="role"/> <id column="status" property="status"/> <id column="regTime" property="regTime"/> <id column="regIp" property="regIp"/> </resultMap> <select id="getAllUser" resultMap="userList"> SELECT * FROM user </select> </mapper>
在dao中建立一個接口,代碼以下
package discuss.dao; import discuss.entity.User; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface IUserDao { public List<User> getAllUser(); }
在service目錄下建立接口IUserService和實現類UserServiceImpl。
package discuss.service; public interface IUserService { }
package discuss.service; import discuss.dao.IUserDao; import discuss.entity.User; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; @Service("userService") public class UserServiceImpl implements IUserService{ @Resource private IUserDao userDao; public List<User> getUser() { return userDao.getAllUser(); } }
在controller目錄下建立UserController類。
package discuss.controller; import discuss.service.UserServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller @RequestMapping("/user") public class UserController { @Autowired public UserServiceImpl userService; @RequestMapping("info") @ResponseBody public List userInfor(){ System.out.println("----------------------------------------------"); System.out.println( userService.getUser().size()); return userService.getUser(); } }
在entity中建立實體類User。
package discuss.entity; import java.sql.Timestamp; public class User { private int id; private String email; private String password; private String username; private String role; private int status; private Timestamp regTime; private String regIp; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public Timestamp getRegTime() { return regTime; } public void setRegTime(Timestamp regTime) { this.regTime = regTime; } public String getRegIp() { return regIp; } public void setRegIp(String regIp) { this.regIp = regIp; } }
點擊右上角符號:
而後點擊綠色的 ‘+’ 號,選擇tomcat。
配置tomcat,肯定計算機上已經安裝了tomcat。
name 隨便取一個。
Application server要點擊Configure找到tomcat的安裝目錄。
端口默認便可。
點擊右下角Fix。
啓動tomcat,訪問http://localhost:8080/user/info.do
訪問路徑能夠在controller中經過註解配置。
成功訪問界面:
Mybatis屬於半自動ORM,在使用這個框架中,工做量最大的就是書寫Mapping的映射文件,因爲手動書寫很容易出錯,咱們能夠利用Mybatis-Generator來幫咱們自動生成文件。
一、準備要自動生成的數據庫表。
數據庫爲discuss users表
CREATE TABLE `users` ( `user_id` int NOT NULL AUTO_INCREMENT , `user_account` varchar(30) NOT NULL , `user_password` char(32) NOT NULL , `user_nickname` varchar(30) NOT NULL , `user_email` varchar(50) NOT NULL , `user_avatar` varchar(30) NOT NULL DEFAULT 'default.jpg' , PRIMARY KEY (`user_id`) ) ;
plates表
CREATE TABLE `plates` ( `plate_id` int NOT NULL AUTO_INCREMENT , `plate_name` varchar(30) NOT NULL , `plate_profile` text NULL , `plate_announcement` text NULL , PRIMARY KEY (`plate_id`) ) ;
themes表
CREATE TABLE `themes` ( `theme_id` int NOT NULL AUTO_INCREMENT , `theme_name` varchar(30) NOT NULL , PRIMARY KEY (`theme_id`) ) ;
posts表
CREATE TABLE `posts` ( `post_id` int NOT NULL AUTO_INCREMENT , `post _title` varchar(30) NOT NULL , `post_author` varchar(30) NOT NULL , `user_id` int NOT NULL , `post_plate` varchar(30) NOT NULL , `plate_id` int NOT NULL , `post_theme` varchar(30) NOT NULL , `theme_id` int NOT NULL , `post_content` text NOT NULL , `post_release_time` datetime NULL , PRIMARY KEY (`post_id`) ) ;
admins
CREATE TABLE `admins` ( `admin_id` int NOT NULL AUTO_INCREMENT , `admin_account` varchar(30) NOT NULL , `admin_nickname` varchar(30) NOT NULL , `admin_password` char(32) NOT NULL , `admin_plate` varchar(30) NOT NULL , `plate_id` int NOT NULL , PRIMARY KEY (`admin_id`) ) ;
comments
CREATE TABLE `comments` ( `comment_id` int NOT NULL AUTO_INCREMENT , `comment_from` varchar(30) NOT NULL , `user_id` int NOT NULL , `commment_time` datetime NOT NULL , `comment_content` text NOT NULL , `post_id` int NOT NULL , PRIMARY KEY (`comment_id`) ) ;
二、準備jar包
關於Mybatis-Generator的下載能夠到這個地址:https://github.com/mybatis/generator/releases
因爲我使用的是Mysql數據庫,這裏須要在準備一個鏈接mysql數據庫的驅動jar包,這個包能夠在http://mvnrepository.com/中搜索下載,選擇本身須要的版本。
新建文件夾src。(生成的文件將在此目錄下)
同時,須要一個配置文件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> <!--數據庫驅動,要對應本身的mysql-connector—java包的版本--> <classPathEntry location="mysql-connector-java-6.0.6-bin.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--數據庫連接地址帳號密碼--> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL= connectionURL="jdbc:mysql://127.0.0.1:3306/discuss?nullCatalogMeansCurrent=true&serverTimezone=UTC&userUnicode=true&characterEncoding=UTF8&useSSL=false" userId="root" password="root"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--生成類的存放位置在src目錄下,個人具體包爲cn.edu.jlu.discuss--> <!--生成Model類存放位置--> <javaModelGenerator targetPackage="cn.edu.jlu.discuss.model" targetProject="src"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--生成映射文件存放位置--> <sqlMapGenerator targetPackage="cn.edu.jlu.discuss.mapping" targetProject="src"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--生成Dao類存放位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="cn.edu.jlu.discuss.dao" targetProject="src"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!--數據庫中的表生成對應的類--> <table tableName="users" domainObjectName="User"> <!--設置主鍵--> <generatedKey column="user_id" sqlStatement="MYSQL" identity="true"/> </table> <table tableName="admins" domainObjectName="Admin"> <generatedKey column="admin_id" sqlStatement="MYSQL" identity="true"/> </table> <table tableName="comments" domainObjectName="Comment"> <generatedKey column="comment_id" sqlStatement="MYSQL" identity="true"/> </table> <table tableName="plates" domainObjectName="Plate"> <generatedKey column="plate_id" sqlStatement="MYSQL" identity="true"/> </table> <table tableName="posts" domainObjectName="Post"> <generatedKey column="post_id" sqlStatement="MYSQL" identity="true"/> </table> <table tableName="themes" domainObjectName="Theme"> <generatedKey column="theme_id" sqlStatement="MYSQL" identity="true"/> </table> </context> </generatorConfiguration>
而後在目錄\mybatis-generator-core-1.3.6\lib地址欄中輸入cmd,回車。
在彈出的控制檯中輸入命名行:(注意本身的jar版本)
java -jar mybatis-generator-core-1.3.6.jar -configfile generatorConfig.xml -overwrite
因爲使用了高版本mysql-connector-java,有時會出現各類各樣的問題,好比須要手動設置時區,手動指定ssh等等問題,這時須要添加修改connectionURL="jdbc:mysql://127.0.0.1:3306/discuss?nullCatalogMeansCurrent=true&serverTimezone=UTC&userUnicode=true&characterEncoding=UTF8&useSSL=false"字段,特別的,主鍵可能會沒法獲取,這是在url字段要加上?nullCatalogMeansCurrent=true
。
生成的文件以下: