eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)

1.创建maven项目

eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第1张图片
eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第2张图片
eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第3张图片
eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第4张图片

这样就可以创建一个maven项目,可以看最下图的目录结构,但是这样的目录结构是不对的,需要做一些修改。
  • 右击项目,点properties ,修改编码格式等
    eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第5张图片
    eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第6张图片
    eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第7张图片
    点击finish,但是此时项目的index.jsp还报错。在pom中引入java ee的jar包即可

<dependency>
<groupId>javaxgroupId>
<artifactId>javaee-apiartifactId>
<version>7.0version>
dependency>

在配一些东西
eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第8张图片

至此,完整的目录结构以及正确的项目完成。

2.ssm整合

下面主要介绍三大框架的整合,至于环境的搭建以及项目的创建,参看上面的博文。这次整合我分了2个配置文件,分别是spring-mybatis.xml,包含spring和mybatis的配置文件,还有个是spring-mvc的配置文件,此外有2个资源文件:jdbc.propertis和log4j.properties。

2.1Maven引入需要的JAR包
此处说明,这里我多引入了后面才能用到的的包,比如 mybatis的自动生成等,每个包都有注释名称,看下。

<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.0modelVersion>
	<groupId>com.spring-mybatisgroupId>
	<artifactId>ssmartifactId>
	<packaging>warpackaging>
	<version>0.0.1-SNAPSHOTversion>
	<name>ssm Maven Webappname>
	<url>http://maven.apache.orgurl>
	
	<properties>  
          
        <spring.version>4.0.2.RELEASEspring.version>  
          
        <mybatis.version>3.2.6mybatis.version>  
          
        <slf4j.version>1.7.7slf4j.version>  
        <log4j.version>1.2.17log4j.version>  
    properties> 
	
	<dependencies>
		<dependency>
			<groupId>junitgroupId>
			<artifactId>junitartifactId>
			<version>3.8.1version>
			<scope>testscope>
		dependency>

		
		<dependency>
			<groupId>javaxgroupId>
			<artifactId>javaee-apiartifactId>
			<version>7.0version>
		dependency>
		
		
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>2.5version>
            <scope>providedscope>
        dependency>
        
        <dependency>
            <groupId>javax.servlet.jspgroupId>
            <artifactId>jsp-apiartifactId>
            <version>2.1version>
            <scope>providedscope>
        dependency>
        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>
		
		
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-coreartifactId>
			<version>${spring.version}version>
		dependency>

		
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-webartifactId>
			<version>${spring.version}version>
		dependency>
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-oxmartifactId>
			<version>${spring.version}version>
		dependency>
		
		 
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-txartifactId>
			<version>${spring.version}version>
		dependency>

		
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-jdbcartifactId>
			<version>${spring.version}version>
		dependency>

		 
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-webmvcartifactId>
			<version>${spring.version}version>
		dependency>
		
		
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.8.4version>
        dependency>
		
		
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-aopartifactId>
			<version>${spring.version}version>
		dependency>
		
		
        <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjweaverartifactId>
            <version>1.8.2version>
        dependency>

		
        <dependency>
            <groupId>javax.annotationgroupId>
            <artifactId>javax.annotation-apiartifactId>
            <version>1.2version>
        dependency>
		
		 
        <dependency>
            <groupId>javax.transactiongroupId>
            <artifactId>javax.transaction-apiartifactId>
            <version>1.2version>
        dependency>
		
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-context-supportartifactId>
			<version>${spring.version}version>
		dependency>

		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-testartifactId>
			<version>${spring.version}version>
		dependency>
		
		<dependency>
			<groupId>org.mybatisgroupId>
			<artifactId>mybatisartifactId>
			<version>${mybatis.version}version>
		dependency>
		
		<dependency>
			<groupId>org.mybatisgroupId>
			<artifactId>mybatis-springartifactId>
			<version>1.2.2version>
		dependency>


		
		<dependency>
			<groupId>mysqlgroupId>
			<artifactId>mysql-connector-javaartifactId>
			<version>5.1.44version>
		dependency>

		
		<dependency>
			<groupId>org.mybatis.generatorgroupId>
			<artifactId>mybatis-generator-coreartifactId>
			<version>1.3.3version>
		dependency>
		
		
		<dependency>
			<groupId>commons-dbcpgroupId>
			<artifactId>commons-dbcpartifactId>
			<version>1.2.2version>
		dependency>

		
		<dependency>
			<groupId>jstlgroupId>
			<artifactId>jstlartifactId>
			<version>1.2version>
		dependency>
		
		
		<dependency>
			<groupId>log4jgroupId>
			<artifactId>log4jartifactId>
			<version>${log4j.version}version>
		dependency>

		
		<dependency>
			<groupId>com.alibabagroupId>
			<artifactId>fastjsonartifactId>
			<version>1.1.41version>
		dependency>

		<dependency>
			<groupId>org.slf4jgroupId>
			<artifactId>slf4j-apiartifactId>
			<version>${slf4j.version}version>
		dependency>

		<dependency>
			<groupId>org.slf4jgroupId>
			<artifactId>slf4j-log4j12artifactId>
			<version>${slf4j.version}version>
		dependency>
		
		
		<dependency>
			<groupId>org.codehaus.jacksongroupId>
			<artifactId>jackson-mapper-aslartifactId>
			<version>1.9.13version>
		dependency>
		
		<dependency>
			<groupId>commons-fileuploadgroupId>
			<artifactId>commons-fileuploadartifactId>
			<version>1.3.1version>
		dependency>
		<dependency>
			<groupId>commons-iogroupId>
			<artifactId>commons-ioartifactId>
			<version>2.4version>
		dependency>
		<dependency>
			<groupId>commons-codecgroupId>
			<artifactId>commons-codecartifactId>
			<version>1.9version>
		dependency>
	dependencies>
	<build>
		<finalName>ssmfinalName>
		
		<plugins>
			
		
            <plugin>
                <groupId>org.eclipse.jettygroupId>
                <artifactId>jetty-maven-pluginartifactId>
                <version>9.2.8.v20150217version>
                <configuration>
                    <httpConnector>
                        <port>80port>
                    httpConnector>
                    <stopKey>shutdownstopKey>
                    <stopPort>9966stopPort>
                configuration>
            plugin>
        plugins>
	build>
project>

2.2、整合SpringMVC
2.2.1、配置spring-mvc.xml
配置里面的注释也很详细,主要是自动扫描控制器,视图模式,注解的启动这三个。

  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
    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-3.1.xsd    
                        http://www.springframework.org/schema/context    
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd    
                        http://www.springframework.org/schema/mvc    
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  
      
    <context:component-scan base-package="com.ssm.controller" />  
    
    <mvc:annotation-driven/>
    
    <mvc:resources location="/resources/**" mapping="/resources"/>
    
      
    <bean id="mappingJacksonHttpMessageConverter"  
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
        <property name="supportedMediaTypes">  
            <list>  
                <value>text/html;charset=UTF-8value>  
            list>  
        property>  
    bean>  
      
    <bean  
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
        <property name="messageConverters">  
            <list>  
                <ref bean="mappingJacksonHttpMessageConverter" />   
            list>  
        property>  
    bean>  
      
      
    <bean id="multipartResolver"    
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    
          
        <property name="defaultEncoding" value="utf-8" />    
          
        <property name="maxUploadSize" value="10485760000" />    
          
        <property name="maxInMemorySize" value="40960" />    
        
        <property name="resolveLazily" value="true"/>
    bean>   
    
    
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="order" value="1">property>
        <property name="mediaTypes">
            <map>
                
                <entry key="json" value="application/json" />
                <entry key="xml" value="application/xml" />
                <entry key="htm" value="text/htm" />
            map>
        property>
        <property name="defaultViews">
            <list>
                
                <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
            list>
        property>
        <property name="ignoreAcceptHeader" value="true">property>
    bean>
    
     
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
          
        <property name="prefix" value="/WEB-INF/pages/" />  
        <property name="suffix" value=".jsp" />  
    bean>  
beans>

这里有两个地方需要注意
1.eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第9张图片
2.eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第10张图片

2.2.2、配置web.xml文件
配置的spring-mvc的Servlet就是为了完成SpringMVC+MAVEN的整合。
web.xml

  
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns="http://java.sun.com/xml/ns/javaee"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
    version="3.0">  
    <display-name>Archetype Created Web Applicationdisplay-name>  
      
    
      
    <filter>  
        <filter-name>encodingFilterfilter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>  
        <async-supported>trueasync-supported>  
        <init-param>  
            <param-name>encodingparam-name>  
            <param-value>UTF-8param-value>  
        init-param>  
    filter>  
    <filter-mapping>  
        <filter-name>encodingFilterfilter-name>  
        <url-pattern>/*url-pattern>  
    filter-mapping>  
      
    
      
  
  
      
    <servlet>  
        <servlet-name>SpringMVCservlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>  
        <init-param>  
            <param-name>contextConfigLocationparam-name>  
            <param-value>classpath:spring-mvc.xmlparam-value>  
        init-param>  
        <load-on-startup>1load-on-startup>  
        <async-supported>trueasync-supported>  
    servlet>  
    <servlet-mapping>  
        <servlet-name>SpringMVCservlet-name>  
          
        <url-pattern>/url-pattern>  
    servlet-mapping>  
    <welcome-file-list>  
        <welcome-file>/index.jspwelcome-file>  
    welcome-file-list>  
  
web-app>

2.2.3、Log4j的配置
为了方便调试,一般都会使用日志来输出信息,Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件,甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。
Log4j的配置很简单,而且也是通用的,下面给出一个基本的配置,换到其他项目中也无需做多大的调整,如果想做调整或者想了解Log4j的各种配置,参看我转载的一篇博文,很详细:http://blog.csdn.net/zhshulin/article/details/37937365

下面给出配置文件目录:
eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第11张图片

log4j.properties

log4j.rootLogger=INFO,Console,File  
#定义日志输出目的地为控制台  
log4j.appender.Console=org.apache.log4j.ConsoleAppender  
log4j.appender.Console.Target=System.out  
#可以灵活地指定日志输出格式,下面一行是指定具体的格式  
log4j.appender.Console.layout = org.apache.log4j.PatternLayout  
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n  
  
#文件大小到达指定尺寸的时候产生一个新的文件  
log4j.appender.File = org.apache.log4j.RollingFileAppender  
#指定输出目录  
log4j.appender.File.File = logs/ssm.log  
#定义文件最大大小  
log4j.appender.File.MaxFileSize = 10MB  
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志  
log4j.appender.File.Threshold = ALL  
log4j.appender.File.layout = org.apache.log4j.PatternLayout  
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
###显示SQL语句部分
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.Java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

2.2.4、使用Jetty测试
eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第12张图片
UserController

package com.ssm.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ssm.entity.User;

@Controller  
public class UserController {  
      
    
    // /user/test?id=1
    @RequestMapping(value="/user/test",method=RequestMethod.GET)  
    public String test(HttpServletRequest request,Model model){  
        int userId = Integer.parseInt(request.getParameter("id"));  
        System.out.println("userId:"+userId);
        User user=null;
        if (userId==1) {
             user = new User();  
             user.setAge(11);
             user.setId(1);
             user.setPassword("123");
             user.setName("javen");
        }
        System.out.println(user);
        model.addAttribute("user", user);  
        return "welcome";  
    }  
}  

user.java

package com.ssm.entity;

public class User {
	private Integer id;

	private String name;

	private String password;

	private Integer age;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password == null ? null : password.trim();
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", password=" + password + ", age=" + age + "]";
	}
}

welcome.jsp

<html>
<head>
	<title>ssmtitle>
head>
<body>
<h2>Hello World!h2>
body>
html>

eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第13张图片
eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第14张图片

在浏览器中输入:http://localhost/user/test?id=1
eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第15张图片

到此 SpringMVC+Maven 整合完毕

2.3 Spring与MyBatis的整合
取消2.2.2 web.xml中注释的代码

2.3.1、建立JDBC属性文件
jdbc.properties(文件编码修改为utf-8,可在eclipse中右击文件选择编码格式)

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test?useSSL=false
username=root
password=
#定义初始连接数  
initialSize=0  
#定义最大连接数  
maxActive=20  
#定义最大空闲  
maxIdle=20  
#定义最小空闲  
minIdle=1  
#定义最长等待时间  
maxWait=60000

当前项目结构
eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第16张图片
2.3.2、建立spring-mybatis.xml配置文件
这个文件就是用来完成spring和mybatis的整合的。这里面也没多少行配置,主要的就是自动扫描,自动注入,配置数据库。注释也很详细,大家看看就明白了。
spring-mybatis.xml

  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
    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-3.1.xsd    
                        http://www.springframework.org/schema/context    
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd    
                        http://www.springframework.org/schema/mvc    
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  
      
    <context:component-scan base-package="com.ssm" />  
    
      
    <bean id="propertyConfigurer"  
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="location" value="classpath:jdbc.properties" />  
    bean>  
  
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
        destroy-method="close">  
        <property name="driverClassName" value="${driver}" />  
        <property name="url" value="${url}" />  
        <property name="username" value="${username}" />  
        <property name="password" value="${password}" />  
          
        <property name="initialSize" value="${initialSize}">property>  
          
        <property name="maxActive" value="${maxActive}">property>  
          
        <property name="maxIdle" value="${maxIdle}">property>  
          
        <property name="minIdle" value="${minIdle}">property>  
          
        <property name="maxWait" value="${maxWait}">property>  
    bean>  
  
      
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        
          
        <property name="mapperLocations" value="classpath:sqlmap/*.xml">property>  
    bean>  
 
      
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="com.ssm.dao" />  
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>  
    bean>  
  
      
    <bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    bean>  
  
beans>

经过以上步骤,我们已经完成了Spring和mybatis的整合,这样我们就可以编写一段测试代码来试试是否成功了。

2.4.1、创建测试用表
在数据库中建立一个测试表,这个表建的很简单,SQL语句为:

-- ----------------------------
-- Table structure for `user_d`
-- ----------------------------
DROP TABLE IF EXISTS `user_d`;
CREATE TABLE `user_d` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(40) NOT NULL,
  `password` varchar(255) NOT NULL,
  `age` int(4) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user_d
-- ----------------------------
INSERT INTO `user_d` VALUES ('1', '黄渤', '345', '24');
INSERT INTO `user_d` VALUES ('2', '徐峥', '123', '10');

增加dao层Mapper,和 xml文件
完成后的目录结构
eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第17张图片

UserMapper.java

package com.ssm.dao;

import com.ssm.entity.User;

public interface UserMapper {
	User getUserById(int i);
}

UserMapper.xml




<mapper namespace="com.ssm.dao.UserMapper">

<resultMap id="user" type="com.ssm.entity.User">
		<id column="id" jdbcType="INTEGER" property="id"/>
		<result column="name" jdbcType="VARCHAR" property="name" />
		<result column="password" jdbcType="VARCHAR" property="password" />
		<result column="age" jdbcType="VARCHAR" property="age" />	
	resultMap>
	
	
	<select id="getUserById"  resultMap="user">
		SELECT * FROM user_d where id=#{id}
	select>
	
mapper>

修改controller方法

package com.ssm.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ssm.dao.UserMapper;
import com.ssm.entity.User;

@Controller
public class UserController {
	@Autowired
	private UserMapper userMapper;

	// /user/test?id=1
	@RequestMapping(value = "/user/test", method = RequestMethod.GET)
	public String test(HttpServletRequest request, Model model) {
		int userId = Integer.parseInt(request.getParameter("id"));
		System.out.println("userId:" + userId);
		User user = null;
		if (userId == 1) {
			user = new User();
			user.setAge(11);
			user.setId(1);
			user.setPassword("123");
			user.setName("javen");
		} else if (userId == 2) {
			// 根据id查询用户信息
			user = userMapper.getUserById(userId);
		}
		System.out.println(user);
		model.addAttribute("user", user);
		return "welcome";
	}
}

修改welcome.jsp

<html>
<head>
	<title>ssmtitle>
head>
<body>
<h2>Hello World!h2>

hello  ${user.name}
body>
html>

在浏览器中输入http://localhost/user/test?id=2 测试
eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第18张图片

注意点:
1.eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第19张图片
2.此时目录结构
eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第20张图片

至此ssm整合完毕!

下面介绍mybatis的自动生成
mybatis的自动生成有多种方法(比如:编写java代码运行、命令提示符运行、eclipse插件运行、maven Plugin运行),几种方法各有优缺点。
这里主要介绍使用 maven Plugin方式运行。
完成后项目结构
eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第21张图片
1,解开pom.xml中的注释
eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第22张图片
2.新建generatorConfig.xml文件
eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第23张图片
generatorConfig.xml




<generatorConfiguration>	
    <context id="MySqlContext" targetRuntime="MyBatis3" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="addRemarkComments" value="true"/>
        commentGenerator>
		
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/test"
                        userId="root"
                        password="111">
        jdbcConnection>
		
        <javaModelGenerator targetPackage="com.ssm.entity" targetProject="src\main\java">
            <property name="trimStrings" value="true" />
        javaModelGenerator>
		
        <sqlMapGenerator targetPackage="sqlmap"  targetProject="src\main\resources"/>
		
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.ssm.dao"  targetProject="src\main\java"/>
		
        <table tableName="user_d" domainObjectName="UserD">
            <generatedKey column="id" sqlStatement="MySql"/>
        table>

    context>
generatorConfiguration>

代码说明:
eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第24张图片

3.运行
eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第25张图片
输入: clean mybatis-generator:generate
eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第26张图片

点击运行 即可
eclipse+maven+ssm框架(Spring+SpringMVC+MyBatis)_第27张图片

自动生成即可完成。

项目下载: https://download.csdn.net/download/zadtimo/10568570

你可能感兴趣的:(ssm,springmvc)