所遇问题:vue+java(springmvc+mybatis)+oracle系统开发之后端springmvc+oracle+mybatis

开启新的路程,持续更新中…
1、创建序列

create sequence KPI_MENUID_seq start with 1 increment by 1 nomaxvalue nominvalue nocycle nocache;

2、oracle语句insert规则描述:
2.1小写变量名需要双引,值需要单引。

INSERT INTO "KPI_Sys_Module" ("moduleId","moduleCode","moduleDesc","moduleSort") VALUES (KPI_MODULEID_SEQ.nextVal,'sysset','系统设置',(SELECT COUNT(*)+1 FROM "KPI_Sys_Module"))

3、数据排序,两者数据对换

UPDATE "KPI_Sys_Module" T1 SET T1."moduleSort"=DECODE("moduleSort",#{sort}+#{direction},#{sort},#{sort},#{sort}+#{direction}, "moduleSort") WHERE T1."moduleSort" IN(#{sort}+#{direction},#{sort})

4、mybatis插入、更新数据后台为number,如果java是int,值为空时会保存成0.如果java是Integer会保存成null;(需要将0转成null则用Integer,如外键、主键。如果需要0则用int或者用if,如数量等。)
5、JSONObject.fromObject,Integer类型null转为0解决方式:

jsonConfig.registerJsonValueProcessor(Integer.class,new IntegerJson());

public class IntegerJson implements JsonValueProcessor {

	@Override
	public Object processArrayValue(Object value, JsonConfig config) {
		String[] obj = {};
		if (value instanceof Integer[]) {
			Integer[] integer = (Integer[]) value;
			obj = new String[integer.length];
			for (int i = 0; i < integer.length; i++) {
				if(integer[i]==null) {
					obj[i] ="";
				}
				
			}
		}
		return obj;
	}

	@Override
	public Object processObjectValue(String key, Object value, JsonConfig config) {
		// TODO Auto-generated method stub
		System.out.println(value+":"+(value==null));
		if (value==null){
			return "";
		}
		return value;
	}

}

6、使用MyBatis查询 返回类型为int,但是当查询结果为空NULL,会报异常。
使用COALESCE(MAX(“menuSort”),0)

SELECT COALESCE(MAX("menuSort"),0) AS "NEXTSORT" FROM "KPI_Sys_Menu" WHERE "menuModule"=#{mId} 

7、springmvc+mybatis使用junit测试
官网使用如下。这种方法将mybatis配置放到单独xml,但是我们实际使用中是将mybatis配置和springmvc配置放在一起的。所以使用如下方法会报错。出错的具体原理不甚明白,但是应该是因为不匹配导致,后期再继续研究。
如果项目中mybaits的配置文件和Spring配置文件整合过了,则下面的代码运行估计会出错,因为一般spring和mybatis整合过之后,mybatis的配置文件基本没有存在的必要了,之前在mybatis中配置的数据源和事务这两个方面,一般的做法都会spring的配置文件,则下面的代码加载mybatis-config.xml的时候,得不到必要的信息,创建的过程中会有问题.所以在这里先给一份mybatis-config.xml单独的配置文件.转自:https://blog.csdn.net/u013412772/article/details/73648537

junit代码:

String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

mybatis配置

<?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>
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="org/mybatis/example/BlogMapper.xml"/>
  </mappers>
</configuration>

springmvc调试:
junit代码:

public class MyTest {
	SyssetServiceImpl SyssetService=null;
	@Before
	public void getBefore(){
		String xmlPath = "config/applicationContext.xml";
		ApplicationContext ac = new FileSystemXmlApplicationContext(xmlPath);
		SyssetService = ac.getBean(SyssetServiceImpl.class);
	}
	@Test
	public void findRouterList(){
		System.out.println(11);
	}
}

配置文件参照springmvc+mybatis配置方式。
8、resultMap包含的元素:

<!--column不做限制,可以为任意表的字段,而property须为type 定义的pojo属性-->

<resultMap id="唯一的标识" type="映射的pojo对象">
  <id column="表的主键字段,或者可以为查询语句中的别名字段" jdbcType="字段类型" property="映射pojo对象的主键属性" />
  <result column="表的一个字段(可以为任意表的一个字段)" jdbcType="字段类型" property="映射到pojo对象的一个属性(须为type定义的pojo对象中的一个属性)"/>
  <association property="pojo的一个对象属性" javaType="pojo关联的pojo对象">
    <id column="关联pojo对象对应表的主键字段" jdbcType="字段类型" property="关联pojo对象的主键属性"/>
    <result  column="任意表的字段" jdbcType="字段类型" property="关联pojo对象的属性"/>
  </association>
  <!-- 集合中的property须为oftype定义的pojo对象的属性-->
  <collection property="pojo的集合属性" ofType="集合中的pojo对象">
    <id column="集合中pojo对象对应的表的主键字段" jdbcType="字段类型" property="集合中pojo对象的主键属性" />
    <result column="可以为任意表的字段" jdbcType="字段类型" property="集合中的pojo对象的属性" />  
  </collection>
</resultMap>

如果collection标签是使用嵌套查询,格式如下:

 <collection column="传递给嵌套查询语句的字段参数" property="pojo对象中集合属性" ofType="集合属性中的pojo对象" select="嵌套的查询语句" > 
 </collection>

注意:标签中的column:要传递给select查询语句的参数,如果传递多个参数,格式为column= ” {参数名1=表字段1,参数名2=表字段2} ;
9、mybatis+oracle批量insert注意事项:
1、不需要VALUES
2、separator=“UNION ALL”

<insert id="save">
		INSERT INTO a(code,name)
		<foreach collection="list" index="index" item="item" separator="UNION ALL">
		SELECT 
			#{item.code,jdbcType=INTEGER},#{item.name,jdbcType=INTEGER}
		FROM dual
		</foreach>
	</insert>

10.choose 报错

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.NumberFormatException: For input string: "s"
### Cause: java.lang.NumberFormatException: For input string: "s

修改前

<choose>
	            <when test="selType=='s'">
	            </when>
	            <when test="selType=='d'">
	            </when>
	            <otherwise>
	            </otherwise>
	        </choose>

修改后

	 <choose>
            <when test="selType=='s'.toString()">
            </when>
            <when test="selType=='d'.toString()">
            </when>
            <otherwise>
            </otherwise>
        </choose>

报错原因说明:个人认为mybatis里面if test中使用时应该等同于java比较两个不同变量是否相同时的逻辑,如果传入的参数为基本类型,则不会出问题。但是如果传入参数是引用类型时,则会存在因类型不一致而在比较时报错。

猜你喜欢

转载自blog.csdn.net/lovexiaobaby/article/details/89084673