02-Maven spring springmvc mybatis安装

看过01的朋友,接下来请看这里,以图开篇
记得Eclipse经常出问题,要时常

  • clean项目
  • clean tomcat
  • clean maven
  • 更要记得 项目【右键】--【Maven】--【Update Project】


    图片.png

    使用MyBatis Generator自动创建代码的代码放到相应的位置即可

  • mybatis是不用创建xxxDaoImpl类的,他的实现在xxxMapper.xml里面
  • 在spring-mybatis.xml配置文件中需要修改文件位置,如果和我的不同

-- domain代码

package com.loujianwei.domain;

public class User {
    private Integer id;

    private String userName;

    private String password;

    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName == null ? null : userName.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

--dao代码

package com.loujianwei.dao;

import com.loujianwei.domain.User;

public interface UserDao {
    int deleteByPrimaryKey(Integer id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}

--service代码

package com.loujianwei.service;

import com.loujianwei.domain.User;

public interface UserService {
        int deleteByPrimaryKey(Integer id);

        int insert(User record);

        int insertSelective(User record);

        User selectByPrimaryKey(Integer id);

        int updateByPrimaryKeySelective(User record);

        int updateByPrimaryKey(User record);
}

--serviceImpl代码

package com.loujianwei.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.loujianwei.dao.UserDao;
import com.loujianwei.domain.User;
import com.loujianwei.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    public int deleteByPrimaryKey(Integer id) {
        // TODO Auto-generated method stub
        return userDao.deleteByPrimaryKey(id);
    }

    public int insert(User record) {
        return userDao.insert(record);
    }

    public int insertSelective(User record) {
        // TODO Auto-generated method stub
        return userDao.insertSelective(record);
    }

    public User selectByPrimaryKey(Integer id) {
        // TODO Auto-generated method stub
        return userDao.selectByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(User record) {
        // TODO Auto-generated method stub
        return userDao.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(User record) {
        return userDao.updateByPrimaryKey(record);
    }

}

--mapper.xml代码

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.loujianwei.dao.UserDao" >
  <resultMap id="BaseResultMap" type="com.loujianwei.domain.User" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="user_name" property="userName" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
    <result column="age" property="age" jdbcType="INTEGER" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, user_name, password, age
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from user_t
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from user_t
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.loujianwei.domain.User" >
    insert into user_t (id, user_name, password, 
      age)
    values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
      #{age,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.loujianwei.domain.User" >
    insert into user_t
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="userName != null" >
        user_name,
      </if>
      <if test="password != null" >
        password,
      </if>
      <if test="age != null" >
        age,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="userName != null" >
        #{userName,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="age != null" >
        #{age,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.loujianwei.domain.User" >
    update user_t
    <set >
      <if test="userName != null" >
        user_name = #{userName,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="age != null" >
        age = #{age,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.loujianwei.domain.User" >
    update user_t
    set user_name = #{userName,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      age = #{age,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

使用测试类,测试mybatis功能 先上代码

package com.loujianwei.gryun;
// spring框架使用的也是这个注解
import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
// 用于序列化 好看
import com.alibaba.fastjson.JSON;

import com.loujianwei.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class) // 表示继承了SpringJUnit4ClassRunner类
@ContextConfiguration(locations = "classpath:spring-mybatis.xml")
public class TestGryun {
    private static Logger logger = Logger.getLogger(TestGryun.class);
    @Resource
    private UserService userService;
    @Test
    public void testDao() {
        System.out.println("aa");
        int aa = userService.deleteByPrimaryKey(1);
        logger.info(JSON.toJSONString(aa));
    }
}

测试错误

重要的是看Caused by 和 最上面的那个红线那里,大概就可以看出来问题,因为是class path resource xxxxx.xml not exist看到这里答案就出来了。

1、
图片.png

修改错误地方为
  • classpath 加载的路径是类路径下 也就是【WEB-INF/classes】目录下
@ContextConfiguration(locations = "classpath:conf/spring-mybatis.xml")

2、看着像是缺少了ibatis包,加上后还是出现这个错误,最后检测路径正确但是创建不成功,有可能是jar包缺少。最后检测缺少了mybatis:3.2.8的包


图片.png

启动tomcat 测试 spring-mvc和spring的整合

1、启动tomcat(把项目加入tomcat中运行)前,需要确保Maven Install了,运行Maven Install会有一些错误如

  • 这个是路径错误,检查配置文件,修改对应位置
  • 如果还错,可以可以看到这里是test代码引起的错误,先注释试试


    图片.png

    2、Maven Install 运行出来的错误没有了,接下来启动tomcat

  • 错误 Error configuring application listener of,查找原因吧
  • 也可以尝试重启Eclipse
  • 查看这里


    图片.png
  • 还有这里


    图片.png
  • 还有这里 看到了这里有个问题 缺少了 Maven管理的jar包位置


    图片.png
  • 应该这样配置


    图片.png
  • 还有这里
    记得Eclipse经常出问题,要时常
  • clean项目
  • clean tomcat
  • clean maven
  • 更要记得 项目【右键】--【Maven】--【Update Project】


    图片.png

    如果上面几个都没有解决,那你再找找度娘

3、又遇见这个问题,唯一高兴的就是 tomcat启动成功了

  • Cannot find class [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter] 这个东西没有加入或者没有找到
  • 可以确认下次路径在有没有 lib文件夹和里面有没有缺少的jar包
    /Users/paycloud110/eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/gryun
  • 缺少了jar包也可以手动添加进来


    图片.png

    4、测试controller是否成功

package com.loujianwei.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LoginController {
    @RequestMapping("/login.do")
    public String login() {
        return "login";
    }
}

浏览器输入

http://localhost:8080/gryun/login.do

返回界面


图片.png

记录使用中的一个Maven现象
项目【右键】--【Maven】--【Update Project】时,
1、会把【WEB-INF/lib】文件夹下的jar包清空。
2、还有如图的地方也会消失,需要自己再次加上去。不晓的有没有可以省略这里配置的


图片.png

3、会遇见Maven中的jar包没有完全加入到lib目录下。可以手动加入,也可以使用上面的 1---2步骤来完成

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,919评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,567评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,316评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,294评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,318评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,245评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,120评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,964评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,376评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,592评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,764评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,460评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,070评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,697评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,846评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,819评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,665评论 2 354

推荐阅读更多精彩内容