Idea 下maven/mybatis/spring、springmvc

 1.在idea下选择maven模式新建一个项目;

点击next后,为项目命名,groupId填入组织的唯一标识一般为域名倒叙,artifactId填入项目唯一标识;

点击next,直到Finish;

idea会自动下载所依赖的包,直到下载完成;注意:此时下载可能会出现问题,一般配置idea环境时默认下载路径是从官网下载,所以会出现下载慢甚至失败的情况,这里把setting.xml中下载地址改成阿里提供,

<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>

这样就得到一个基本的样板;

2.关于如何使用Mybatis逆向工程自动生成代码,在mybaits中我们使用generatorConfig.xml,生成包括DAO层、model层、mapping SQL映射文件;在main/resources中创建generatorConfig.xml文件

详细注解(参考:http://blog.csdn.net/gebitan505/article/details/51788894):

  1. <?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>
    <context id="testTables" targetRuntime="MyBatis3">
    <commentGenerator>
    <!-- 是否去除自动生成的注释 true:是 : false:否 -->
    <property name="suppressAllComments" value="true"/>
    </commentGenerator>
    <!--mysql数据库连接的信息:驱动类、连接地址、用户名、密码 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
    connectionURL="jdbc:mysql://localhost:3306/ssm" userId="root"
    password="admin">
    </jdbcConnection>

    <!--oracle配置-->
    <!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver" connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg"
    userId="yycg"
    password="yycg">
    </jdbcConnection> -->

    <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,
    为 true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal -->
    <javaTypeResolver>
    <property name="forceBigDecimals" value="false"/>
    </javaTypeResolver>

    <!-- targetProject:生成model类的位置,重要!! -->
    <javaModelGenerator targetPackage="com.www.model" targetProject="src\main\java">
    <!-- enableSubPackages:是否让schema作为包的后缀 -->
    <property name="enableSubPackages" value="false"/>
    <!-- 从数据库返回的值被清理前后的空格 -->
    <property name="trimStrings" value="true"/>
    </javaModelGenerator>

    <!-- targetProject:mapper映射xml文件生成的位置,重要!! -->
    <sqlMapGenerator targetPackage="com.www.mapper"
    targetProject="src\main\java">
    <property name="enableSubPackages" value="false"/>
    </sqlMapGenerator>

    <!-- targetPackage:mapper接口生成的位置,重要!! -->
    <javaClientGenerator type="XMLMAPPER"
    targetPackage="com.www.dao"
    targetProject="src\main\java">
    <property name="enableSubPackages" value="false"/>
    </javaClientGenerator>

    <!-- 指定数据库表,要生成哪些表,就写哪些表,要和数据库中对应,不能写错! -->
    <table tableName="items"></table>

    <table tableName="user"></table>
    </context>
    </generatorConfiguration>

 

 

可能出现问题"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd";

 

解决办法:复制出现红色字体的路径,然后点击左上角file-setting-- schemas and DtDs,点击加号。把路径考进去就可以了 。

自动生成文件前需要正确连接数据库,下面介绍数据库表建立;使用的是navicat premium;

navicat如何连接mysql:

1.首先安装数据库,

2.运行“Navicat Premium”,进入Navicat Premium管理界面。

打开“localhost_3306”连接,连接本地MySQL数据库服务器。创建并打开数据库“test”,然后在test上新建表,保存时会提醒你输入表名,如user;

数据库建立成功;

在pox.xml中添加plugins;

 

<?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>ssm</name>
<groupId>com.www.ssm</groupId>
<artifactId>ssm</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.3</version>
<configuration>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

<dependencies>
<!--dependency>
<groupId>com.www.ssm</groupId>
<artifactId>[the artifact id of the block to be mounted]</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency-->
</dependencies>

</project>
3、

 


 可以看到左边自动生成了dao model mapper

 

 

 待完善。。。。。

 

 

 

posted @ 2017-06-02 16:59  babyla  阅读(197)  评论(0)    收藏  举报