Mybatis的两种开发方式

时间:2019-01-17
本文章向大家介绍Mybatis的两种开发方式,主要包括Mybatis的两种开发方式使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

方式一:原始dao的方式:

通过定义Dao接口,写dao实现类,在Dao实现类里获得SqlSession,通过SqlSession来操作数据库。

步骤一:创建Dao接口

public interface UserDao {
	public User findUserById(int id);
	public void addUser(User user);
}

步骤二:创建Dao的实现类

public class UserDaoImpl implements UserDao{
	
	private SqlSessionFactory sqlSessionFactory;
	public UserDaoImpl(SqlSessionFactory sqlSessionFactory){
		this.sqlSessionFactory = sqlSessionFactory;
	}
	public User findUserById(int id) {
		SqlSession session = sqlSessionFactory.openSession();
		User user = session.selectOne("test.selectUserById",id);
		return user;
	}
	public void addUser(User user) {
		SqlSession session = sqlSessionFactory.openSession();
		int count = session.insert("test.insertUser",user);
		session.commit();
	}
}

步骤三:测试

public class TestUserDao {
	InputStream input =null;
	SqlSession session=null;
	SqlSessionFactory factory=null;
	@Before
	public void initSession() throws IOException{
			//找到主配置文件
			String resource = "SqlMapConfig.xml";
			//读取主配置文件
			input= Resources.getResourceAsStream(resource);
			//通过主配置文件获取session工厂
			factory= new SqlSessionFactoryBuilder().build(input);
			//获得session
			session = factory.openSession();
	}
	@Test
	public void testfindUserById(){
		UserDaoImpl userDaoImpl = new UserDaoImpl(factory);
		User user = userDaoImpl.findUserById(1);
		System.out.println(user);
	}
	@After
	public void afterSession(){
		if(session!=null){
			session.close();
		}
		if(input!=null){
			try {
				input.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

方式二:mapper接口代理方式

步骤一:创建一个Student类

create table student(
       id int auto_increment primary key,       
       sname varchar(20),       
       birthday date,       
       sex varchar(2),       
       address varchar(30)
)

步骤二:创建Student实体类

public class Student {
	private int id;
	private String sname;
	private Date birthday;
	private String sex;
	private String address;
 }

步骤三:创建映射文件 config/mapper/StudentMapper.xml,通过需要创建一个相同名字的接口。

1>命名空间等于接口的全路径

2>根据sql映射写对应的接口

<mapper namespace="org.ljy.mybatis.mapper.StudentMapper">
    <select id="selectStudentsById" parameterType="int" resultType="org.ljy.mybatis.mapper.Student" >
      select * from student where id = #{id}
    </select> 
</mapper>

步骤四:把映射文件加入到主配置文件中

<mappers>
     <mapper resource="mapper/UserMapper.xml"/>
     <mapper resource="mapper/StudentMapper.xml"/>
 </mappers>

步骤五:测试

public class TestStudent {
	
	SqlSession session = null;
	InputStream  inputStream = null;
	@Before
	public void initSession() throws IOException{
			//找到主配置文件
			String resource = "SqlMapConfig.xml";
			//读取主配置文件
			inputStream = Resources.getResourceAsStream(resource);
			//通过主配置文件获得session工厂
			SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
			//获得session
			session = factory.openSession();
	}
	@Test
	public void testFindStudentById(){
		StudentMapper studentMapper = session.getMapper(StudentMapper.class);
		//接口代理  通过接口产生了一个对应接口的实现类,系统创建的接口实现类
		Student student = studentMapper.findStudentById(1);
		System.out.println(student);
	}
	@After
	public void close(){
		if(session != null){
			session.close();
		}
		if(inputStream != null){
			try {
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}