package com.bean;
public class User {
private Integer id;
private String username;
private String password;
private Integer phone;
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;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getPhone() {
return phone;
}
public void setPhone(Integer phone) {
this.phone = phone;
}
}
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.mapper=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
package com.mapper;
import java.net.PasswordAuthentication;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.bean.User;
public interface UserMapper {
@Insert("insert into user(username, password, phone) values(#{username},#{password},#{phone})")
int addUser(User user);
@Delete("delete from user where id=#{id}")
int deleteUserById(int id);
@Update("update user set username=#{username},password=#{password} where id=#{id}")
int updateById(User user);
@Select("select * from user")
//id表示哪个字段是主键
//column对应的数据库的字段名
//property对应Java实体的属性名
//如果两个都一致,可以不用配置,直接@Results({})
@Results({
@Result(id=true,property="id",column="id"),
@Result(property="username",column="username"),
@Result(property="password",column="password"),
@Result(property="phone",column="phone")
})
List selectUser();
@Select("select * from user where username like '%${value}%'")
List selectUsersBylike(String username);
//如果传递两个参数,就需要使用@Param对参数命名,其中@Param("")括号中的值必须跟sql语句中的一致
@Select("select * from user where username=#{username} and password=#{password}")
User selectUserBytwo(@Param("username")String username1,@Param("password")String password1);
}
package com.test;
import java.io.IOException;
import java.io.InputStream;
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 org.apache.tools.ant.types.CommandlineJava.SysProperties;
import org.junit.Test;
import com.bean.User;
import com.mapper.UserMapper;
public class Test1 {
@Test
public void add() throws IOException {
//第一步 读取配置文件mybatis-config.xml
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
//第二步 创建会话工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//第三步 生成会话
SqlSession session = sessionFactory.openSession();
//第四步代理映射(获取mapper接口的代理对象)
UserMapper userMapper=session.getMapper(UserMapper.class);
User user=new User();
user.setUsername("二狗子");
user.setPassword("123456");
user.setPhone(110);
int i=userMapper.addUser(user);
if(i>0)
System.out.println("插入成功");
else
System.out.println("插入失败");
session.commit();
session.close();
}
}
package com.test;
import java.io.IOException;
import java.io.InputStream;
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 org.junit.Test;
import com.mapper.UserMapper;
public class Test2 {
@Test
public void delete() throws IOException
{
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sessionFactory.openSession();
UserMapper userMapper=session.getMapper(UserMapper.class);
int i=userMapper.deleteUserById(1);
if(i>0)
System.out.println("删除成功");
else
System.out.println("删除失败");
session.commit();
session.close();
}
}
package com.test;
import java.io.IOException;
import java.io.InputStream;
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 org.junit.Test;
import com.bean.User;
import com.mapper.UserMapper;
import javassist.expr.NewArray;
public class Test3 {
@Test
public void update() throws IOException
{
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sessionFactory.openSession();
UserMapper userMapper=session.getMapper(UserMapper.class);
User user=new User();
user.setId(2);
user.setUsername("指针");
user.setPassword("1111111");
int i=userMapper.updateById(user);
if(i>0)
System.out.println("修改成功");
else
System.out.println("修改失败");
session.commit();
session.close();
}
}
package com.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 org.junit.Test;
import com.bean.User;
import com.mapper.UserMapper;
public class Test4 {
@Test
public void select() throws IOException
{
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sessionFactory.openSession();
//第四步代理映射
UserMapper userMapper=session.getMapper(UserMapper.class);
List users=userMapper.selectUser();
for(User user:users)
{
System.out.print(user.getId()+"\t");
System.out.print(user.getUsername()+"\t");
System.out.print(user.getPassword()+"\t");
System.out.println(user.getPhone());
}
}
@Test
public void selectUserByLike() throws IOException
{
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sessionFactory.openSession();
UserMapper userMapper=session.getMapper(UserMapper.class);
List users=userMapper.selectUsersBylike("哈");
for(User user:users)
{
System.out.print(user.getId()+"\t");
System.out.print(user.getUsername()+"\t");
System.out.print(user.getPassword()+"\t");
System.out.println(user.getPhone());
}
}
@Test
public void selectUserBytwo() throws IOException
{
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sessionFactory.openSession();
UserMapper userMapper=session.getMapper(UserMapper.class);
User user=userMapper.selectUserBytwo("哈哈", "1111111");
System.out.print(user.getId()+"\t");
System.out.print(user.getUsername()+"\t");
System.out.print(user.getPassword()+"\t");
System.out.println(user.getPhone());
}
}