标签:查询 print 多表 parameter .class tar art ges dtd
按照下面类型建立article表
package com.ij34.model; public class Article { private int articleid; private User user; private String title; private int userid; private String content; public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public int getArticleid() { return articleid; } public void setId(int id) { this.articleid = id; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
<?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.ij34.mybatis.UserMapper"> <select id="selectUser" parameterType="int" resultType="com.ij34.model.User"> select * from users where id=#{id}; </select> <update id="updateUser" keyProperty="id"> update users set name=#{name},age=#{age} where id=#{id} </update> <insert id="insertUser" > insert into users(name,age)values(#{name},#{age}) </insert> <delete id="deleteUser"> delete from users where name=#{name} </delete> <resultMap type="com.ij34.model.Article" id="resultAticleList"> <id property="articleid" column="articleid"/> <result property="userid" column="userid"/> <result property="title" column="title"/> <result property="content" column="content"/> <association property="user" javaType="com.ij34.model.User"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="age" column="age"/> </association> </resultMap> <select id="selectArticle" parameterType="int" resultMap="resultAticleList"> select * from users,article where users.id=article.userid and users.id=#{id} </select> </mapper>
package com.ij34.bean; import java.io.IOException; import java.io.InputStream; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.ij34.model.*; public class Test { public static void main(String[] args) throws IOException { String resource ="com/ij34/mybatis/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session=sqlSessionFactory.openSession(); try { // User user=session.selectOne("com.ij34.mybatis.UserMapper.selectUser", 8); // user.setName("小小A"); // user.setAge(24); // session.update("com.ij34.mybatis.UserMapper.updateUser", user); // session.commit(); // System.out.println(user); // User user=new User(); // user.setName("测试01"); // user.setAge(19); // session.insert("com.ij34.mybatis.UserMapper.insertUser", user); // session.commit(); // int user=session.delete("com.ij34.mybatis.UserMapper.deleteUser","测试01"); // session.commit(); // System.out.println(user); List<Article> articles=session.selectList("com.ij34.mybatis.UserMapper.selectArticle",1); for(Article article:articles){ System.out.println(article.getArticleid()+":"+article.getTitle()+":"+article.getContent()+" "+article.getUser()); } // UserMapper mapper=session.getMapper(UserMapper.class); // List<Article> articles=mapper.selectarticle(1); // for(Article article:articles){ // System.out.println(article.getId()+":"+article.getTitle()+":"+article.getContent()+" "+article.getUser()); // } } finally { // TODO: handle finally clause session.close(); } } }
有点问题 article返回id不该是0
标签:查询 print 多表 parameter .class tar art ges dtd
原文地址:http://www.cnblogs.com/tk55/p/6661786.html