数据库
方式一:XML
按照下面类型建立article表
Article.java
packagecom.ij34.model;public classArticle {private intid;privateUser user;privateString title;privateString content;publicString getContent() {returncontent;
}public voidsetContent(String content) {this.content =content;
}public intgetId() {returnid;
}public void setId(intid) {this.id =id;
}publicUser getUser() {returnuser;
}public voidsetUser(User user) {this.user =user;
}publicString getTitle() {returntitle;
}public voidsetTitle(String title) {this.title =title;
}
}
UserMapper.xml
颜色aid重点标出部分很重要,必须使用别名,不要最后结果获得article的id全是输入的id
/p>
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select * from users where id=#{id};
update users set name=#{name},age=#{age} where id=#{id}
insert into users(name,age)values(#{name},#{age})
delete from users where name=#{name}
select users.id,users.name,users.age,article.id aid,article.title,article.content from users,article
where users.id=article.userid and users.id=#{id}
Test.java
packagecom.ij34.bean;importjava.io.IOException;importjava.io.InputStream;importjava.util.List;importorg.apache.ibatis.io.Resources;importorg.apache.ibatis.session.SqlSession;importorg.apache.ibatis.session.SqlSessionFactory;importorg.apache.ibatis.session.SqlSessionFactoryBuilder;import com.ij34.model.*;public classTest {public static void main(String[] args) throwsIOException {
String resource="com/ij34/mybatis/mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory= newSqlSessionFactoryBuilder().build(inputStream);
SqlSession session=sqlSessionFactory.openSession();try{
List articles=session.selectList("com.ij34.mybatis.UserMapper.selectarticle",1);for(Article article:articles){
System.out.println(article.getId()+":"+article.getTitle()+":"+article.getContent()+" "+article.getUser());
}
}finally{//TODO: handle finally clause
session.close();
}
}
}
结果
方式二:通过接口
结构
xml
mybatis-config.xml
/p>
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
UserMapper.xml
对应于接口UserMapper
/p>
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select * from users where id=#{id};
update users set name=#{name},age=#{age} where id=#{id}
insert into users(name,age)values(#{name},#{age})
delete from users where name=#{name}
select users.id,users.name,users.age,article.id aid,article.title,article.content from users,article
where users.id=article.userid and users.id=#{id}
model
User.java
packagecom.ij34.model;public classUser {private intid;privateString name;private intage;public intgetId() {returnid;
}public void setId(intid) {this.id =id;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}publicString toString() {return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
Article.java
packagecom.ij34.model;public classArticle {private intid;privateUser user;privateString title;privateString content;publicString getContent() {returncontent;
}public voidsetContent(String content) {this.content =content;
}public intgetId() {returnid;
}public void setId(intid) {this.id =id;
}publicUser getUser() {returnuser;
}public voidsetUser(User user) {this.user =user;
}publicString getTitle() {returntitle;
}public voidsetTitle(String title) {this.title =title;
}
}
UserMapper.java
packagecom.ij34.model;importjava.util.List;public interfaceUserMapper {/*// @Select("select * from User where id=#{id}")
public User selectUser(int id);
// @Update("update User set name=#{name},age=#{age} where id=#{id}")
public void updateUser(User user);*/
public List selectarticle(intid);
}
测试
packagecom.ij34.bean;importjava.io.IOException;importjava.io.InputStream;importjava.util.List;importorg.apache.ibatis.io.Resources;importorg.apache.ibatis.session.SqlSession;importorg.apache.ibatis.session.SqlSessionFactory;importorg.apache.ibatis.session.SqlSessionFactoryBuilder;importcom.ij34.model.Article;importcom.ij34.model.UserMapper;public classTest {public static void main(String[] args) throwsIOException {
String resource="com/ij34/mybatis/mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory= newSqlSessionFactoryBuilder().build(inputStream);
SqlSession session=sqlSessionFactory.openSession();try{
UserMapper mapper=session.getMapper(UserMapper.class);
List 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();
}
}
}
结果