Mybatis无法运行更新

时间:2016-04-04 12:05:17

标签: mybatis

updateOneArticle.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
  Created by IntelliJ IDEA.
  User: jack
  Date: 16-4-4
  Time: 下午3:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>一个文章的修改界面</title>
</head>
<s:form action="updateonearticleaction">
    <s:textfield  label="标题" name="article.title" />
    <s:textfield label="类别"  name="article.archive" />
    <s:textarea label="内容" name="article.content" cols="20" rows="3"/>
    <s:submit   value="修改"/>
</s:form>
<s:debug/>
</body>
</html>

struts.xml中

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <constant name="struts.devMode" value="true"/>
    <package name="default" namespace="/" extends="struts-default">
        <action name="useraction" class="main.java.cn.qingtianr.action.UserAction">
            <result name="success">
                backend.jsp
            </result>
            <result name="fail">
                fail.jsp
            </result>
        </action>

        <action name="articleaction" class="main.java.cn.qingtianr.action.ArticleAction" method="writeArticle">
            <result name="lookarticle">
                lookarticle.jsp
            </result>
        </action>

        <action name="articleactionshow" class="main.java.cn.qingtianr.action.ArticleAction" method="showArticle">
            <result name="lookarticle">
                lookarticle.jsp
            </result>
        </action>
        <action name="getarticleaction" class="main.java.cn.qingtianr.action.ArticleAction" method="getOneArticle">
            <result name="post">
                post.jsp
            </result>
        </action>
        <action name="managearticleaction" class="main.java.cn.qingtianr.action.ArticleAction" method="manageArticle">
            <result name="managearticle">
                managearticle.jsp
            </result>
        </action>
        <action name="updatearticleaction" class="main.java.cn.qingtianr.action.ArticleAction" method="updateArticle">
            <result name="updateOneArticle">
                updateOneArticle.jsp
            </result>
        </action>
        <action name="updateonearticleaction" class="main.java.cn.qingtianr.action.ArticleAction" method="updateOneArticle">
        <result name="success">
            success.jsp
        </result>
        </action>
    </package>
</struts>

ArticleDao.java

package main.java.cn.qingtianr.dao;

import main.java.cn.qingtianr.model.Article;

import java.util.ArrayList;

/**
 * Created by jack on 16-3-30.
 */
public interface ArticleDao {
    public Boolean addArticle(Article article);
    public ArrayList<Article> showArticle();
    public Article getOneArticle(String title);
    public int updateArticle(Article article);
}

ArticleDao.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="main.java.cn.qingtianr.dao.ArticleDao">

    <update id="updateArticle" parameterType="main.java.cn.qingtianr.model.Article">
        UPDATE article SET archive = #{archive} , content =#{content} WHERE title = #{title}
    </update>

</mapper>

ArticleServiceImpl.java

public boolean updateArticle(Article article) {
    try{
        System.out.println(article.getTitle());
        System.out.println(article.getArchive());
        System.out.println(article.getContent());
        int s = MybatisSqlSessionFactory.getSession().getMapper(ArticleDao.class).updateArticle(article);
        System.out.println("hello It is in updateArticle");
        System.out.println(s);
    }
    catch (Exception e)
    {
        e.printStackTrace();
        throw e;
    }
    finally
    {
        MybatisSqlSessionFactory.closeSession();
    }
    return true;
}

SQL

CREATE TABLE `article` (
  `title` varchar(20) NOT NULL,
  `archive` varchar(20) DEFAULT NULL,
  `content` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`title`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Article.java

package main.java.cn.qingtianr.model;

/**
 * Created by jack on 16-3-30.
 */
public class Article {
    private String title;
    private String archive;
    private String content;

    public Article(){

    }

    public Article(String title, String archive, String content) {
        this.title = title;
        this.archive = archive;
        this.content = content;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getArchive() {
        return archive;
    }

    public void setArchive(String archive) {
        this.archive = archive;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

我是mybatis.today的新手,我遇到了一个问题。我使用 ArticleServiceImp.java 来更新我的数据。但它不起作用。 我保证当它在公共布尔updateArticle(文章文章)时,文章是我想要的。因为我打印它。并且当打印时var s = 1。

我认为问题出在 Article.xml

我hava花了很多时间来解决它。所以任何人都可以帮助我吗?thx。

并且没有例外。

1 个答案:

答案 0 :(得分:0)

在ArticleDao.xml中

尝试将变量更改为#{Article.archive}而不是#{archive}