第一种方式步骤
1.maven引入依赖包
org.mybatismybatis3.4.4mysqlmysql-connector-java5.1.26
2.创建mybatis-config.xml
3.创建数据库表/创建实体类
public class User {
private int id;
private String name;
private int age;
}
4.创建实体类配置文件(xml)
insert into user (id,name,age) values (#{id},#{name},#{age})
5.测试
public void test() throws Exception{
String resource = "mybatis-config.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = sqlSessionFactory.openSession();
User user = new User();
user.setName("xxx");
user.setAge(10);
sqlSession.insert(User.class.getName()+".insert",user);
sqlSession.commit();
sqlSession.close();
}
第二种方式步骤
1.maven引入依赖包
2.创建mybatis-config.xml
3.创建数据库表/创建实体类
4.创建实体类Mapper接口
public interface UserDao {
public void insert(User user);
}
5.创建实体类配置文件(xml)
insert into user (id,name,age) values (#{id},#{name},#{age})
6.测试
public void test() throws Exception{
String resource = "mybatis-config.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = sqlSessionFactory.openSession();
User user = new User();
user.setName("xxx");
user.setAge(10);
UserDao mapper = sqlSession.getMapper(UserDao.class);
mapper.insert(user);
sqlSession.commit();
sqlSession.close();
}
加载配置文件
mybatis-config.xml
增删改查/分页查询
注意:增加/删除/修改 都可以使用update方法?
增加
insert into user (id,name,age) values (#{id},#{name},#{age})
sqlSession.insert(User.class.getName()+".insert",user);
增加时返回id
insert into teacher (name,age) values(#{name},#{age})
Teacher teacher = new Teacher();
teacher.setName("shuaige");
teacher.setAge(20);
sqlSession.insert(Teacher.class.getName()+".insert", teacher);
System.out.println(teacher.getId());
注意:
useGeneratedKeys="true" 设置是否使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的主键字段中
keyProperty="id" 指定JavaBean的主键字段
删除
delete from user where id=#{id}
sqlSession.delete(User.class.getName()+".delete",user);
delete from user where id=#{id}
sqlSession.delete(User.class.getName()+".delete",7);
修改
update user set name=#{name},age=#{age} where id=#{id}
sqlSession.update(User.class.getName()+".update",user);
查询
根据id查询
User user = sqlSession.selectOne(User.class.getName()+".getById", 8);
根据名字模糊查询
List list = sqlSession.selectList(User.class.getName()+".getByName", "%shuaige%");
分页查询
Map dataMap = new HashMap();
dataMap.put("name", "%shuaige%");
dataMap.put("start", 0);
dataMap.put("size", 3);
List list = sqlSession.selectList(User.class.getName()+".getByPage", dataMap);
第二种
RowBounds bounds = new RowBounds(0,5);
List list = sqlSession.selectList(User.class.getName()+".getLimit", bounds);
List集合
sql:
create table users(
id int primary key auto_increment comment '主键',
name varchar(20) not null default '' comment '姓名'
)engine=innoDB default charset=utf8;
create table address(
id int primary key auto_increment comment '主键',
address varchar(20) not null default '' comment '地址',
u_id int not null,
constraint fk_user_address foreign key (u_id) references users(id)
)engine=innoDB default charset=utf8;
User.xml
insert into users (name) values (#{name})
User.java
public class User implements Serializable{
private static final long serialVersionUID = 4980066222353642286L;
private int id;
private String name;
private List address = new ArrayList();
}
Address.xml
insert into address (address,u_id) values (#{address},#{user.id})
Address.java
public class Address {
private int id;
private String addr;
private User user;
}
插入
User user = new User();
user.setName("帅哥");
user.getAddress().add("北京");
user.getAddress().add("广州");
sqlSession.insert(User.class.getName()+".insert",user);
for(String str : user.getAddress()){
Address address = new Address();
address.setAddress(str);
address.setUser(user);
sqlSession.insert(Address.class.getName()+".insert", address);
}
查询
User user = sqlSession.selectOne(User.class.getName()+".getOne", 1);
Set集合
sql:
create table user(
id int primary key auto_increment comment '主键',
name varchar(20) not null default '' comment '姓名'
)engine=innoDB default charset=utf8;
create table address(
addr varchar(20) not null default '' comment '地址',
u_id int not null comment '外键',
constraint fk_u_a_id foreign key (u_id) references user(id)
)engine=innoDB default charset=utf8;
User.xml
insert into user (name) values (#{name})
User.java
public class User implements Serializable{
private static final long serialVersionUID = 4980066222353642286L;
private int id;
private String name;
private Set address = new HashSet();
}
Address.xml
insert into address(addr,u_id) values (#{addr},#{user.id})
Address.java
public class Address {
private String addr;
private User user;
}
测试
插入
User user = new User();
user.setName("帅哥");
user.getAddress().add("北京");
user.getAddress().add("广州");
sqlSession.insert(User.class.getName()+".insert",user);
for(String str : user.getAddress()){
Address address = new Address();
address.setAddr(str);
address.setUser(user);
sqlSession.insert(Address.class.getName()+".insert", address);
}
查询
User user = sqlSession.selectOne(User.class.getName()+".getOne", 1);
Map集合
创建日期处理器
public class DateTypeHandler implements TypeHandler {
public String getResult(ResultSet rs, String columnName) throws SQLException {
Date value = rs.getTimestamp(columnName);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(value);
}
}
在全局映射文件mybatis-config.xml配置处理器
在映射文件中使用
注意:做专门的数据显示处理
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,Given 1->2->3->3->4->4->5,