package com;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.pojo.Article;
import com.pojo.User;
public class test1 {
SqlSessionFactory sesionfactory = SessionFactory.getSession();
public void getUserList(String userName) {
SqlSession session = sesionfactory.openSession();
try {
IUserOperation userOperation = session
.getMapper(IUserOperation.class);
List<User> users = userOperation.selectUsers("summer");
for (User user : users) {
System.out.println(user.getId() + ":" + user.getUserName()
+ ":" + user.getUserAddress());
}
} finally {
session.close();
}
}
public void getUser(int id) {
SqlSession session = sesionfactory.openSession();
try {
IUserOperation userOperation = (IUserOperation) session
.getMapper(IUserOperation.class);
User user = userOperation.selectUserByID(1);
System.out.println(user.getUserAddress());
System.out.println(user.getUserName());
} finally {
session.close();
}
}
public void getUsersPage(int start, int len) {
SqlSession session = sesionfactory.openSession();
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("start", start);
map.put("length", len);
try {
IUserOperation userOperation = session
.getMapper(IUserOperation.class);
List<User> users = userOperation.getUsersPage(map);
for (User user : users) {
System.out.println(user.getId() + ":" + user.getUserName());
}
} finally {
session.close();
}
}
public void getUsersPage1(int start,int len){
SqlSession session = sesionfactory.openSession();
try {
IUserOperation userOperation=session.getMapper(IUserOperation.class);
List<User> users = userOperation.getUsersPage1("id","userName",start,len);
for(User user:users){
System.out.println(user.getId()+":"+user.getUserName()
);
}
} finally {
session.close();
}
}
public void getUserPage2(int start,int len){
SqlSession session = sesionfactory.openSession();
try {
List<Integer> list=new ArrayList<Integer>();
list.add(start);
list.add(len);
IUserOperation userOperation=session.getMapper(IUserOperation.class);
List<User> users = userOperation.getUserPageList(list);
for(User user:users){
System.out.println(user.getId()+":"+user.getUserName()
);
}
} finally {
session.close();
}
}
public void addUser() {
User user = new User();
user.setUserAddress("人民广场");
user.setUserName("飞鸟");
user.setUserAge("80");
SqlSession session = sesionfactory.openSession();
try {
IUserOperation userOperation = session
.getMapper(IUserOperation.class);
userOperation.addUser(user);
();
System.out.println("当前增加的用户 id为:" + user.getId());
} finally {
session.close();
}
}
public void updateUser() {
// 先得到用户,然后修改,提交。
SqlSession session = sesionfactory.openSession();
try {
IUserOperation userOperation = session
.getMapper(IUserOperation.class);
User user = userOperation.selectUserByID(3);
user.setUserAddress("原来是魔都的浦东创新园区");
userOperation.updateUser(user);
();
} finally {
session.close();
}
}
/**
* 删除数据,删除一定要 commit.
*
* @param id
*/
public void deleteUser(int id) {
SqlSession session = sesionfactory.openSession();
try {
IUserOperation userOperation = session
.getMapper(IUserOperation.class);
userOperation.deleteUser(id);
();
} finally {
session.close();
}
}
public void getUserArticles(int userid) {
SqlSession session = sesionfactory.openSession();
try {
IUserOperation userOperation = session
.getMapper(IUserOperation.class);
List<Article> articles = userOperation.getUserArticles(userid);
for (Article article : articles) {
System.out.println(article.getTitle() + ":"
+ article.getContent() + ":作者是:" + article.getUser());
}
} finally {
session.close();
}
}
public static void main(String[] args) {
test1 testUser = new test1();
testUser.getUser(1);
testUser.getUserList("%");
// testUser.getUserArticles(1);
//testUser.getUsersPage(1, 1);
//testUser.getUsersPage1(1,1);
testUser.getUserPage2(1,2);
// testUser.addUser();
// testUser.updateUser();
// testUser.deleteUser(5);
}
}