发表于: 2020-01-10 23:27:31

1 1131


今天完成的事情:

修改昨天的postman测试 404问题

其中一个原因

controller代码命名成了  controler   

导致spring_mvc无法开启注解扫描


测试数据: 


1. 查询分页正常   就是显示乱码 后面修改


@ResponseBody
@RequestMapping(method = RequestMethod.GET)
public Map findAll(){

Map<String,Object> map = new HashMap<>();
   
   try {
List<onework> onework = one.findAll();
       map.put("onework", onework);
       map.put("code", 200);
       map.put("msg","进入主页面成功");
       return map;
   }catch (Exception e) {

map.put("code",201);
       map.put("msg","进入主页面失败");
       return map;
   }

}


2. 根据姓名和状态查询


@ResponseBody
@RequestMapping(value = "/searchAll",method = RequestMethod.GET)
public Map secrchAll(@RequestParam(value = "oneworkName") String oneworkName,
                    @RequestParam(value = "state") int state) {

Map<String,Object> map = new HashMap<>();

   try {
List<onework> onework = one.searchAll(oneworkName, state);

       map.put("onework", onework);
       map.put("code", 200);
       map.put("msg","查询成功");

       return map;

   }catch (Exception e) {

map.put("code",201);
       map.put("msg","查询失败");

       return map;


<!--    姓名和状态-->
<select id="searchAll"  resultType="pojo.onework" >
   SELECT * FROM onework
<where>
<if  test="onework_name ! = null and onwork_name !=''">
    onework_name LIKE '%' #{oneworkName}'%'
</if>
<if  test="onework_name ! = null and onwork_name !=''">
   AND state = #{state}
</if>
</where>
   LIMIT 0,10
</select>


postman失败


这应该是 sql语句有问题  但我没看出来


3.更改   

<!--更改名字和状态-->
   <update id="putTwo" parameterType="pojo.onework">
       update onework
<set>
      <choose>
       <when test="onework_name!=null and onework_name!='' ">
           onework_name =#{oneworkName}
</when>
          <otherwise>
           state=#{state}
</otherwise>
      </choose>>
</set>
       where id = #{id}
</update>
@ResponseBody
@RequestMapping(method = RequestMethod.PUT)
public Map putTwo(onework onework) {
Map<String,Object> map = new HashMap<>();

   try {
one.putTwo(onework);
       map.put("code", 200);
       map.put("msg", "更改成功");
       return map;

   } catch (Exception e) {
map.put("code", 201);
       map.put("msg", "更改失败");
       return map;
   }

201 失败  难道也是SQL语法有问题



4.post增加  

<!--    添加-->
   <insert id="addName" parameterType="pojo.onework" >
       INSERT INTO onewrok (id,onework_name,state,create_at,create_by,update_at,update_by)
values (#{id},#{oneworkName},#{state},#{createAt},#{createBy},#{updateAt},#{updateBy})
</insert>
@ResponseBody
@RequestMapping(method = RequestMethod.POST)
public Map addName(onework onework){
Map<String,Object> map = new HashMap<>();

   try {
one.addName(onework);
       map.put("code", 200);
       map.put("msg","添加成功");
       return map;

   }catch (Exception e) {
map.put("code",201);
       map.put("msg","添加失败");
       return map;
   }

201 错误 


5.delete 删除    成功运行

@ResponseBody
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public Map deleteId(@PathVariable int id) {
Map<String,Object> map = new HashMap<>();

   try {
one.deleteId(id);
       map.put("code", 200);
       map.put("msg", "删除成功");
       return map;

   } catch (Exception e) {
map.put("code", 201);
       map.put("msg", "删除失败");
       return map;
   }


post  put  get(2个参数)    有问题   


个人觉得原因是 


1.  动态SQL语法有问题


2. postman提交参数方式不对


3. 数据库字段  带下划线  和  实体类  没对应上


但还没试出来..



关于数据库带下划线   实体类不带的问题




java实体类



整理了下网上搜索的几种对应方法


1. mybatis开启驼峰映射


首先在mybatas框架中的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>

<settings>

<setting name="mapUnderscoreToCamelCase" value="true" />

</settings>

</configuration>  



然后在   applicatiponContext.xml配置

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

        <property name="dataSource" ref="dataSource"/>

        <property name="configLocation" value="classpath:mybatis-config.xml" />

        <!-- 自动扫描mapping.xml文件 -->

        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>

    </bean>


开启后  自动对应  



2. resultMap  

<resultMap type="order" id="orderResultMap">

        <!-- 定义主键 ,非常重要。如果是多个字段,则定义多个id -->

        <!-- property:主键在pojo中的属性名 -->

        <!-- column:主键在数据库中的列名 -->

        <id property="id" column="id" />

        <!-- 定义普通属性 -->

        <result property="userId" column="user_id" />

        <result property="number" column="number" />

        <result property="createtime" column="createtime" />

        <result property="note" column="note" />

    </resultMap>


3. as别名

select id, 

user_name as userName, 

user_sex as userSex,

user_age as userAge 

from user



4. 使用jsonproperty注解   也可以使2个相同

 

我现在用的是这个  


明天计划的事情:


解决问题,完成项目



返回列表 返回列表
评论

    分享到