1.使用idea搭建mybatis—基于接口
新建maven工程,然后在pom.xml导入依赖。
mysql
mysql-connector-java
6.0.6
org.mybatis
mybatis
3.4.6
2.mybtis 的工作流程
//加载配置文件,sqlsessionFactorybuilder创建sqlsessionfactory对象
Reader reader = Resources.getResourceAsReader("config/mybatis-conf.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
//1.创建sqlsessionFactory
SqlSessionFactory sqlSessionFactory = DBtools.getSqlSessionFactory();
//2.创建SqlSession
SqlSession session = sqlSessionFactory.openSession();
//3.session 中创建相应的接口代理类,即mapper对象
UserBeanMapper userBeanMapper = session.getMapper(UserBeanMapper.class);
3.整个工程如下
配置mybatis的配置文件mybatis-conf.xml:
mysql.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=root
userMapper.xml
insert into user values(#{id},#{name},#{age})
delete from user where name=#{name}
update user set name=#{name},set age=#{age} where id=#{id}
UserBean.java
package com.jun.entity;
/**
* @author jun
* @date 2019/1/31
*/
public class UserBean {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
DBtools.java工具类创建sqlsessionfactory
package com.jun;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.Reader;
/**
* @author jun
* @date 2019/1/31
*/
public class DBtools {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
Reader reader = Resources.getResourceAsReader("config/mybatis-conf.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSessionFactory(){
return sqlSessionFactory;
}
}
UserBeanMapper.java
package com.jun.dao;
import com.jun.entity.UserBean;
import java.util.List;
/**
* @author jun
* @date 2019/1/31
*/
public interface UserBeanMapper {
UserBean queryUserByName(String name);
List queryAll();
int insertUser(UserBean userBean);
int deleteUserByName(String name);
int updateUserById(UserBean userBean,int id);
}
进行验证
public static void main(String[] args){
//1.创建sqlsessionFactory
SqlSessionFactory sqlSessionFactory = DBtools.getSqlSessionFactory();
//2.创建SqlSession
SqlSession session = sqlSessionFactory.openSession();
//3.session 中创建相应的接口代理类,即mapper对象
UserBeanMapper userBeanMapper = session.getMapper(UserBeanMapper.class);
System.out.println(userBeanMapper.queryAll());
try {
System.out.println(userBeanMapper.deleteUserByName("jun"));
UserBean u1 = new UserBean();
u1.setAge(28);
u1.setName("king");
u1.setId(3);
System.out.println(userBeanMapper.insertUser(u1));
session.commit();//一定要提交,不然所有增删改操作不会生效的
System.out.println(userBeanMapper.queryAll());
}catch (Exception e){
session.rollback();//回滚
}
}
4.结合ssm整合来看,mybatis提供了一个整合包,来整合,不过原理大概也是这样子的。都是用sqlsessionfactory来进行持久化。
以上内容,有些是看到别人写的,加上自己也开始学,先记下笔记,也方便后来人看。
http://www.mybatis.org/mybatis-3/zh/configuration.html#typeAliases
详细请看官网更加好。