Mybatis的Dao层实现
1、传统方式实现
创建接口com.dao.UserMapper
package com.dao;
import com.domain.User;
import java.io.IOException;
import java.util.List;
public interface UserMapper {
public List<User> findAll() throws IOException;
}
创建文件com.dao.impl.UserMapperImpl
package com.dao.impl;
import com.dao.UserMapper;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class UserMapperImpl implements UserMapper {
public List<Object> findall() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession=sqlSessionFactory.openSession();
List<Object> userList = sqlSession.selectList("userMapper.findAll");
return userList;
}
}
创建文件com.dao.service.userService
package com.dao.service;
import com.dao.UserMapper;
import com.domain.User;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class userService {
public static void main(String[] args) throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession=sqlSessionFactory.openSession();
UserMapper mapper=sqlSession.getMapper(UserMapper.class);
List<User> all = mapper.findAll();
System.out.println(all);
}
}
2、接口代理开发方式介绍
3、接口代理开发的方式代码实现
把impl包删掉,userService文件下的main代码删掉
配置文件UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.UserMapper">
<!--查询操作-->
<select id="findAll" resultType="user">
select * from user
</select>
</mapper>
修改UserService
package com.dao.service;
import com.dao.UserMapper;
import com.domain.User;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class userService {
public static void main(String[] args) throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession=sqlSessionFactory.openSession();
UserMapper mapper=sqlSession.getMapper(UserMapper.class);
List<User> all = mapper.findAll();
System.out.println(all);
}
}
再来测试根据id进行查询
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dao.UserMapper">
<!--查询操作-->
<select id="findAll" resultType="user">
select * from user
</select>
<!--根据id进行查询-->
<select id="findById" parameterType="int" resultType="user">
select * from user where id=#{id}
</select>
</mapper>
接口
package com.dao;
import com.domain.User;
import java.io.IOException;
import java.util.List;
public interface UserMapper {
public List<User> findAll() throws IOException;
public User findById(int id);
}
package com.dao.service;
import com.dao.UserMapper;
import com.domain.User;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class userService {
public static void main(String[] args) throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession=sqlSessionFactory.openSession();
UserMapper mapper=sqlSession.getMapper(UserMapper.class);
List<User> all = mapper.findAll();
System.out.println(all);
User user = mapper.findById(1);
System.out.println(user);
}
}