maven配合MyBatis-CMEU快速搭建ssm框架

MyBatis-CMEU全称为:MyBatis Config Mapper Entity Util
简单来说MyBatis-CMEU是一款使用Javafx开发的MyBatis逆向工程帮助工具,
该工具支持Oracle,Mysql,SqlServer,PostgreSql等数据库的生成。

简单说:只需要创建好数据库和表,使用这个工具会自动生成service层,dao层和maapper.xml,
对表的增删改查基本操作方法都会生成,让程序员更加专注业务和控制层

教程目录:

  1. 搭建一个maven的项目
  2. 配置文件
  3. 使用MyBatis-CMEU生成
  4. 测试

1. 搭建一个maven的项目

这里不会教毕竟不是这个主题,可以查看链接:搭建一个maven项目

2. 配置文件

ssm配置文件的配置,参考我的另一篇博客:快速搭建ssm框架

只需要看
pom.xml

其他的先可以不动,后面会贴出代码。

3. 使用MyBatis-CMEU生成

接下来就是有关MyBatis-CMEU的使用了,这里是图文教学,
注意的是MyBatis-CMEU2.0的环境要求比较高需要JDK1.8.66以上,我用的是JDK1.9

数据库
3.1 数据库,我已经创建好了(以前的数据库),表的字段也需要,我这里等下就用user表做实验

3.2 MyBatis-CMEU2.0下载可以去这个官方Q群:519714660
3.2.1

下载后解压,然后点击到里面找到如图所示:
这里写图片描述

双击箭头所指之处,就可以运行这个程序了

这里写图片描述

中文的,下面是引入一个数据库,把user表生成service层、dao层、mapper.xml

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

按照上面图片步骤,去文件夹可以找到生成的结果
这里写图片描述

这里写图片描述

可以看到service接口层基本方法已经生成,其他层也一样,这里就不一一打开,你们可以自己去看。

3.3 后续搭建
把这些文件复制到你项目的位置,可能会出现导包的错误,改改包名就行

这里写图片描述

文件结构,common,dao,entity,service文件都是直接复制过来的,
缺少一个controller层,我这里就不弄了,主要是测试service能不能行。


jdbc.properties代码

jdbc.driverclass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/as?useUnicode=true&useSSL=false&characterEncoding=utf8
jdbc.username=root
jdbc.password=

mybatis-config.xml(这个会自动生成,可以复制过来,把数据源配置去掉就行)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <mappers>
        <mapper resource="com/main/user/dao/UserMapper.xml" />
    </mappers>
</configuration>

applicationContext.xml

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx  
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- 自动扫描 -->
    <context:component-scan base-package="com.main" />

     <!-- JDBC.properties属性配置文件 -->  
    <context:property-placeholder location="classpath:jdbc.properties"/>  

    <!-- 数据源DataSource -->  
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <property name="driverClassName" value="${jdbc.driverclass}"/>  
        <property name="url" value="${jdbc.url}"/>  
        <property name="username" value="${jdbc.username}"/>  
        <property name="password" value="${jdbc.password}"/>  
    </bean> 


    <!-- 配置mybatis的sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"  />      
        <!-- mybatis配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.main.*.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 配置事务通知属性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!-- 定义事务传播属性 -->
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="upd*" propagation="REQUIRED" />
            <tx:method name="edit*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="new*" propagation="REQUIRED" />
            <tx:method name="set*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="change*" propagation="REQUIRED" />
            <tx:method name="check*" propagation="REQUIRED" />
            <tx:method name="register*" propagation="REQUIRED" />
            <tx:method name="login*" propagation="REQUIRED" />
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />
            <tx:method name="search*" propagation="REQUIRED" read-only="true" />
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />
            <tx:method name="load*" propagation="REQUIRED" read-only="true" />
            <tx:method name="*" propagation="REQUIRED" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <!-- 配置事务切面 -->
    <aop:config>
        <aop:pointcut id="serviceOperation"
            expression="(execution(* com.main.service.*.*(..)))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
    </aop:config>

</beans>

spring-mvc.xml

<?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-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 使用注解的包,包括子集 -->
    <context:component-scan base-package="com.main.*.controller"/>

    <!-- 设置默认配置方案 -->
    <mvc:annotation-driven/>
    <!-- 使用默认的Servlet来响应静态文件 -->
    <mvc:default-servlet-handler />

     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jsonHttpMessageConverter" />
            </list>
        </property>
    </bean>

    <bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>application/json;charset=UTF-8</value>
            </list>
        </property>

    </bean>

    <!-- 视图解析器 -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"></property>
    </bean>




</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <!-- 加载Spring容器配置 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 设置Spring容器加载所有的配置文件的路径 ,默认加载的是web-inf下的applicationcontext.xml文件,此处是自定义-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>

    <!-- 配置SpringMVC核心控制器 -->
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 默认加载的是wen-inf下的XXX-servlet.xml(springMVC-servlet.xml)文件,此处自定义 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:spring-mvc.xml</param-value>
        </init-param>
        <!-- 启动加载一次 -->  
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!--为DispatcherServlet建立映射 -->
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <!-- 此处可以可以配置成*.do -->
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <!-- 防止Spring内存溢出监听器 -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>

    <!-- 解决工程编码过滤器 -->
    <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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

4. 测试数据库操作是否成功

这里写图片描述

先创建一个测试类,文件位置要注意。
另外在serviceImpl层需要在类名上加入注解:
@Service(“userService”)
private UserDao userDao;上加上
@Resourse
再把这个userDao的get,set方法去掉
因为我们采用的是注解模式
这里写图片描述

测试类ServiceTest.java代码

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.main.user.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:/applicationContext.xml")
public class ServiceTest{

    @Resource
    private UserService userService;

    @Test
    public void TestOrder(){

        //根据ID删除一条记录,这里删除一条ID为2的记录,删除成功输出1
        System.out.println(userService.deleteUserById(2));

    }

}

测试结果:

这里写图片描述

其他方法自行测试,这里就不一一做测试了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值