使用ssm框架+pgsql

笔者在实践ssm+pgsql的过程中遇到一些坑,特意作文,以免后人被坑。

1. 关于pgsql在框架中的使用,首先就是解决用工具自动生成mapper.xml和实体类,

 

   这张图片上的内容都能下载到,不做过多解释,主要还是 genertorConfig_mysql.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="postgresql.jar" />  
  
  <context id="jqtek" > 
      <commentGenerator>  
        <property name="suppressDate" value="false"/>  
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->  
            <property name="suppressAllComments" value="false"/>  
        </commentGenerator>  
  
     <jdbcConnection 
	driverClass="org.postgresql.Driver" 
	connectionURL="jdbc:postgresql://postgresql01.public.test.baozun.cn:5432/db_ehr"  
 	userId="user_ehr" password="user_ehr" />
	 <!-- mybatis里专门用来处理NUMERIC和DECIMAL类型的策略 -->    
        <javaTypeResolver>    
            <property name="forceBigDecimals" value="false"/>    
        </javaTypeResolver>
	
	
    <javaModelGenerator targetPackage="com.xx.entity" targetProject="MyGent\java">
    	<property name="trimStrings" value="true" />
    </javaModelGenerator>
    
    <sqlMapGenerator targetPackage="com.xx.mapping" targetProject="MyGent\resources">
    </sqlMapGenerator>
    
    <javaClientGenerator targetPackage="com.baozun.dao" targetProject="MyGent\java" type="XMLMAPPER">
    </javaClientGenerator>
	                              <!--  配置生成的实体类不需要辅助的examplateModel -->
	<table tableName="tb_user" enableCountByExample="false" enableUpdateByExample="false" 
               enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
			   <generatedKey column="id" sqlStatement="PostgreSql" identity="true" />
	</table>
	

  </context>
</generatorConfiguration>
****************************bat 批处理文件的书写(随便写的 txt文件,然后将后缀改成.bat)

del /f /q /s MyGent
java -jar mybatis-generator-core-1.3.1.jar -configfile generatorConfig_mysql.xml
pause

******************************************************************************************************

 2. 当生成了 mapper,xml文件时,由于pgsql和oracle一样,没有自增的主键,所以需要自己先在数据库去自定义一个自增的 序列

 CREATE TABLE "public"."tb_user" (
"id" int4 DEFAULT nextval('test_c_id_seq'::regclass) NOT NULL,
"user_name" varchar(25) COLLATE "default",
"user_password" varchar(25) COLLATE "default",
CONSTRAINT "tb_user_pkey" PRIMARY KEY ("id")
)
WITH (OIDS=FALSE)
;

  1. CREATE SEQUENCE test_c_id_seq 
  2. START WITH 1  
  3. INCREMENT BY 1  
  4. NO MINVALUE  
  5. NO MAXVALUE  
  6. CACHE 1;  
  7.   
  8. alter table tb_user alter column id set default nextval('test_c_id_seq'); 

 ****************************************************************************************

 具体在mapper.xml中写法就是 

<insert id="insert" parameterType="com.baozun.entity.TbUser" >
  <selectKey  resultType="java.lang.Integer" keyProperty="id" order="BEFORE" >
    SELECT nextval('test_c_id_seq')
  </selectKey>

  insert into tb_user (user_name, user_password)
  values (#{userName,jdbcType=VARCHAR}, #{userPassword,jdbcType=VARCHAR})
</insert>

*******************************************************************************************************

 关于mybatis的分页插件的使用

先在pom.xml中 引入jar包

 <pagehelper.version>3.4.2</pagehelper.version>
<mybatis.paginator.version>1.2.15</mybatis.paginator.version>
<dependency>
<groupId>com.github.miemiedev</groupId>
<artifactId>mybatis-paginator</artifactId>
<version>${mybatis.paginator.version}</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
***********************
在mybatis-config.xml中配置分页插件,
<!-- 配置分页插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型Oracle,MySQL,MarinDBName,SQLite,PostareSQL六种数据库 -->
<property name="dialect" value="postgresql"/>
</plugin>
</plugins>

具体使用就是在 controller 或者其他,

PageHelper.startPage(item.getPageIndex(),item.getPageSize());
List<TbUser> userList = userService.getUserList();
PageInfo<TbUser> pageInfo=new PageInfo<TbUser>(userList);

*******************************************************************************************************

对于 springMVC中的@RequestBody,前端如果是 使用的 ajax传值,那么是一定要写消息头,而且一定要将

传入的数据和 声明的一定要保持一致,

function displayModule(){
    $("#dislist").show();
    var params = {"pageSize":3,"pageIndex":1,"orgsId":"2"};
    $.ajax({
        type:"POST",
        url:"/User/userlistByAjax",
        contentType:"application/json",
        dataType:"json",
        data:JSON.stringify(params),
        success:function(data){
            var flag=data.flag;
            var msg=data.msg;
            if(flag==true){
                var itemList=data.object;
                $.each(itemList,function(i,item){
                    $("#hidenTd").append(
                        '<tr>' +
                        '<td>'+item.id+'</td>' +
                        '<td>' +item.userName+'</td>' +
                        '<td><a href="/User/'+item.id+'/delete" >删除</a></td>' +
                        '<td><a href="/User/'+item.id +'/detail">详细</a></td>' +
                        ' </tr>' ) ;
                });
            }else{
                alert(msg);
            }
            alert("成功");
        },
        error:function(e) {
            alert("出错:"+e);
        }
    });
}

我在前台接受可以直接使用 内部静态类  @RequestBody  UserService.GetUserAndOrgsInfo  item

public interface UserService{
    List<Map> selectMap(Map map);
    static class GetUserAndOrgsInfo extends RequestBase{
        public int orgsId;
    }
}

***********内部静态类的属性必须和传入的键值保持一致,

 *******************************************************************************************************

使用mybatis时,实现复杂查询,那么就是 传入是Map ,传出是List<Map>

dao层:  List<Map> selectByName(Map map);
 .xml层 : <select id="selectByName" parameterType="java.util.HashMap" resultType="java.util.HashMap">
     SELECT * from
       tb_user a
    where  a.user_name=#{mapName}
  </select>

controller使用:  Map map=new HashMap();
map.put("mapName","名字1");

************************************************************************************


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值