springboot mybatis项目遇到的问题

springboot mybatis项目遇到的问题,第1张

一些小问题
  1. 数据库中Longint对应java中long类型
  2. Byte数据类型先转Integer,比较好处理,每次重新generator都要记得改
  3. mybatis generator多次使用容易重复创建,删掉xml文件重新generator即可
  4. controller层一般做不侵入业务逻辑的工作,一般是入口,不能相互调用。因此对于涉及到几张表 *** 作的一个功能,例如用户注册要在user、role、relationship中添加数据,则统一在service层完成,service可用互相调用
@Transactional注解解决事务完整性

在try catch中try中有return不算报错
若是try-catch-finally
如try{
return 20
} cathch{}
fianlly{println 1}
会打印:
1
20

我对@Transactional的用法

	@Transactional(rollbackFor = {Exception.class}) //保证事务完整性
    @Override
    public int userRegister(String accountName, String userPassword, String mail,Integer role){//TODO
        //检查该用户名是否被占用
        User hasUser = loginValidation(accountName,userPassword);
        if(hasUser !=null){
            return 20;//被使用就返回20
        }
        boolean flag = true;
        try {
            //这里是要保证完整性的 *** 作集合组成一个事务
        }catch(Exception e){
            //todo: 日志功能
            flag = false;
        }
        if(!flag){
            //回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return -1;

注意

  • @Transactional 在private和protected修饰的方法上不会生效
  • 看别人的博客说不加(rollbackFor = {Exception.class}),则方法默认只会在抛出RuntimeException时执行回滚
mybatis generator逆向工程的使用

记得在在pom中添加相关配置


DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <properties resource="mybatis-generator/jdbc.properties"/>
    
    <classPathEntry location="D:\maven_warehouse\repository\mysql\mysql-connector-java\5.1.47\mysql-connector-java-5.1.47.jar"/>
    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            
            <property name="suppressAllComments" value="true"/>
        commentGenerator>

        
        <jdbcConnection
                driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/e-sports?serverTimezone=UTC&useSSL=true"
                userId="root"
                password="123456">
        jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        javaTypeResolver>

        
        <javaModelGenerator targetPackage="com.sports.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        javaModelGenerator>

        
        <sqlMapGenerator targetPackage="com.sports.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        sqlMapGenerator>

        
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.sports.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        javaClientGenerator>

        
        <table tableName="user" domainObjectName="User"
               enableCountByExample="true"
               enableUpdateByExample="true"
               enableDeleteByExample="true"
               enableSelectByExample="true"
               selectByExampleQueryId="true">
            <property name="useActualColumnNames" value="false" />
            <generatedKey column="id" sqlStatement="Mysql" identity="true" />
        table>
        
    context>
generatorConfiguration>


生成的文件如下

生成的xml文件若在recourse里面会出现no bounding的错误,即userMapper找不到生成的xml文件。在网上找了很多办法试过都没有用,最后只能把xml文件和userMapper放在同一路径下才行

报 Result Maps collection already contains的错误应该是generator生成的xml文件生成重复了,删除掉重新generator即可

具体用法
@Override
    public User loginValidation(String accountName,String userPassword) {
        //System.out.println(userMapper.selectByExample());
        UserExample example = new UserExample();
        example.createCriteria().andUserAccountNameEqualTo(accountName).andUserPasswordEqualTo(userPassword);
        List<User> userList = userMapper.selectByExample(example);
        return userList.get(0);
    }

以上这条等等价于 select * from user where userAccountName=“accountName” and userPassword=“userPassword”
返回值类型是List
关于Criteria:我的理解是相当于where关键字,Criteria包含一个Cretiron的集合,每一个Criteria对象内包含的Cretiron之间是由AND连接的,是逻辑与的关系

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/797652.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)

保存