首页 > 其他 > 详细

SSM使用MybatisGenerator生成dao层

时间:2019-12-03 18:42:57      阅读:207      评论:0      收藏:0      [点我收藏+]

官网下载

mybatis generator

下载generator的release版本mybatis-generator-core-1.4.0-bundle.zip

https://github.com/mybatis/generator/releases

SSM

构建基础spring mvc

https://www.cnblogs.com/aeolian/p/11950980.html

整合Mybatis

除了mybatis官网下的mybatis.jar

还需要mybatis-spring.jar

spring 与 mybatis对应关系表

我使用的版本(之前用spring3.1整合失败,容器初始化报错升级成4.2.4的)

<spring.version>4.2.4.RELEASE</spring.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis.spring.version>1.2.2</mybatis.spring.version>

springconfig.xml配置

    <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 加载mybatis的全局配置文件,里面写有分页 -->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
    </bean>
    
    <!--扫描mapper-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.autumn.mapping" />
    </bean>

技术分享图片

mybatis generator

generator.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>  
<!-- 数据库驱动-->  
    <classPathEntry  location="D:\workspace6_5\HjzzAuthPlatform\mysql-connector-java-5.1.22-bin.jar"/>  
    <context id="DB2Tables"  targetRuntime="MyBatis3">  
        <commentGenerator>  
            <property name="suppressDate" value="true"/>  
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->  
            <property name="suppressAllComments" value="false"/>  
        </commentGenerator>  
        <!--数据库链接URL,用户名、密码 -->  
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" 
            connectionURL="jdbc:mysql://222.222.221.138:3306/authplatform?useUnicode\=true&amp;characterEncoding\=UTF-8" 
          userId="root" password="root">  
        </jdbcConnection>  
        <javaTypeResolver>  
            <property name="forceBigDecimals" value="false"/>  
        </javaTypeResolver>  
        <!-- 生成pojo的包名和位置-->  
        <javaModelGenerator targetPackage="com.autumn.model" targetProject="src">  
            <property name="enableSubPackages" value="true"/>  
            <property name="trimStrings" value="true"/>  
        </javaModelGenerator>  
        <!-- 生成映射文件xml的包名和位置-->  
        <sqlMapGenerator targetPackage="com.autumn.mapper" targetProject="src">
            <property name="enableSubPackages" value="true"/>  
        </sqlMapGenerator>  
        <!-- 生成DAO接口的包名和位置-->  
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.autumn.mapper" targetProject="src">
            <property name="enableSubPackages" value="true"/>  
        </javaClientGenerator>
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->  
        <table tableName="account" domainObjectName="Account" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="auth_role" domainObjectName="Auth_role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="auth_system" domainObjectName="Auth_system" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        
    </context>  
</generatorConfiguration>

把下载的mybatis-generator-core-1.4.0.jar、generator.xml(需要指定mysql-connector-java-5.1.22-bin.jar)放入项目跟目录下面。

技术分享图片

用java命令生成dao、mapper、pojo

java -jar mybatis-generator-core-1.4.0.jar -configfile generator.xml -overwrite

技术分享图片

技术分享图片

如果是maven,generator.xml放在resource下面,pom.xml中用如下配置,maven的targetProject和普通的myeclipse项目不一样。

      <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
      </dependency>
      <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.2</version>
      </plugin>

测试

成功整合ssm。

之前系统一直是ss + JdbcTemplate封装库,最近一个项目特别适用用mybatis的自动生成代码,做一些简单的增删改查。于是在里面又整合了mybatis,整合成功。

@Controller
@RequestMapping("/common")
public class CommonController {

    //Spring JDBCTemplate
    @Resource(name="commonDao")
    private CommonDao dao = null;;
    
    //Mybatis
    @Autowired
    AccountMapper accountMapper = null;
    
    @RequestMapping(value="/testJDBCTemplate",method={RequestMethod.GET,RequestMethod.POST})
    public ModelAndView testJDBCTemplate(){
        ModelAndView mv = new ModelAndView("/jsp/success");
        dao.execute("INSERT INTO `account` (`sysid`, `userid`, `aut_userId`, `username`, `userpwd`) VALUES (‘aaa‘, ‘aeolian‘, 32323, ‘fff‘, ‘ggg‘)", new Object[]{});
        mv.addObject("msg", "success");
        return mv;
    }
    
    @RequestMapping(value="/testMybatis",method={RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Object testMybatis(){
        AccountKey pam = new AccountKey();
        pam.setSysid("1");
        pam.setUserid("pingtai");
        return accountMapper.selectByPrimaryKey(pam);
    }
}

遇到的问题

ERROR [localhost-startStop-1] org.springframework.web.context.ContextLoader

spring容器初始化失败。

解决方法:升级spring、mybatis、mybatis-spring到可以相互兼容的版本。

org.apache.ibatis.binding.BindingException

调用dao接口是提示绑定失败。

解决方法:mybatis接口和xml文件不再同一个包内。

 

SSM使用MybatisGenerator生成dao层

原文:https://www.cnblogs.com/aeolian/p/11975803.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!