Mybatis框架三:DAO层开发、Mapper动态代理开发
2018-06-18 02:42:11来源:未知 阅读 ()
这里是最基本的搭建:http://www.cnblogs.com/xuyiqing/p/8600888.html
接下来做到了简单的增删改查:http://www.cnblogs.com/xuyiqing/p/8601506.html
但是发现代码重复过多等问题
接下来整合并实现DAO开发:
一:原始DAO开发:
package dao; import pojo.User; public interface UserDao { public User selectUserById(Integer id); }
package dao; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import pojo.User; public class UserDaoImpl implements UserDao { //注入 private SqlSessionFactory sqlSessionFactory; public UserDaoImpl(SqlSessionFactory sqlSessionFactory) { this.sqlSessionFactory = sqlSessionFactory; } //通过用户ID查询一个用户 public User selectUserById(Integer id){ SqlSession sqlSession = sqlSessionFactory.openSession(); return sqlSession.selectOne("test.findUserById", id); } //通过用户名称模糊查询 public List<User> selectUserByUsername(Integer id){ SqlSession sqlSession = sqlSessionFactory.openSession(); return sqlSession.selectList("test.findUserById", id); } }
测试类:
package junit; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Before; import org.junit.Test; import dao.UserDao; import dao.UserDaoImpl; import pojo.User; public class DaoTest { public SqlSessionFactory sqlSessionFactory; @Before public void before() throws Exception { String resource = "sqlMapConfig.xml"; InputStream in = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); } @Test public void testDao() throws Exception { UserDao userDao = new UserDaoImpl(sqlSessionFactory); User user = userDao.selectUserById(10); System.out.println(user); } }
但是还是发现代码重复、浪费资源等问题:
于是想到Mapper动态代理开发:
package mapper; import pojo.User; public interface UserMapper { //遵循四个原则 //接口 方法名 == User.xml 中 id 名 //返回值类型 与 Mapper.xml文件中返回值类型要一致 //方法的入参类型 与Mapper.xml中入参的类型要一致 //命名空间 绑定此接口 //这里如果返回的是一个对象,则调用selectOne方法 //如果是List,则调用selectlist方法 public User findUserById(Integer id); }
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"> <!-- 写Sql语句 --> <mapper namespace="mapper.UserMapper"> <!-- 通过ID查询一个用户 --> <select id="findUserById" parameterType="Integer" resultType="pojo.User"> select * from user where id = #{v} </select> <!-- //根据用户名称模糊查询用户列表 #{} select * from user where id = ? 占位符 ? == '五' ${} select * from user where username like '%五%' 字符串拼接 --> <select id="findUserByUsername" parameterType="String" resultType="pojo.User"> select * from user where username like "%"#{haha}"%" </select> <!-- 添加用户 --> <insert id="insertUser" parameterType="pojo.User"> <selectKey keyProperty="id" resultType="Integer" order="AFTER"> select LAST_INSERT_ID() </selectKey> insert into user (username,birthday,address,sex) values (#{username},#{birthday},#{address},#{sex}) </insert> <!-- 更新 --> <update id="updateUserById" parameterType="pojo.User"> update user set username = #{username},sex = #{sex},birthday = #{birthday},address = #{address} where id = #{id} </update> <!-- 删除 --> <delete id="deleteUserById" parameterType="Integer"> delete from user where id = #{vvvvv} </delete> </mapper>
主配置文件sqlMapConfig.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="xuyiqing" /> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/UserMapper.xml"/> </mappers> </configuration>
测试类:
package junit; 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 mapper.UserMapper; import pojo.User; public class MybatisMapperTest { @Test public void testMapper() throws Exception { String resource = "sqlMapConfig.xml"; InputStream in = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); SqlSession sqlSession = sqlSessionFactory.openSession(); //SqlSession自动为接口生成一个实现类 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = userMapper.findUserById(10); System.out.println(user); } }
通常情况下,建议使用Mapper动态代理开发
配置上还可以完善下:
主配置文件:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="jdbc.properties"/> <typeAliases> <package name="pojo"/> </typeAliases> <!-- 和spring整合后 environments配置将废除 --> <environments default="development"> <environment id="development"> <!-- 使用jdbc事务管理 --> <transactionManager type="JDBC" /> <!-- 数据库连接池 --> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments> <!--Mapper.xml:写Sql语句的文件的位置 --> <mappers> <!-- 这种方式要求包和对应xml文件在一个包中 --> <package name="mapper"/> </mappers> </configuration>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=xuyiqing
在user.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"> <!-- 写Sql语句 --> <mapper namespace="mapper.UserMapper"> <!-- 通过ID查询一个用户 --> <select id="findUserById" parameterType="Integer" resultType="User"> select * from user where id = #{v} </select> <!-- //根据用户名称模糊查询用户列表 #{} select * from user where id = ? 占位符 ? == '五' ${} select * from user where username like '%五%' 字符串拼接 --> <select id="findUserByUsername" parameterType="String" resultType="User"> select * from user where username like "%"#{haha}"%" </select> <!-- 添加用户 --> <insert id="insertUser" parameterType="pojo.User"> <selectKey keyProperty="id" resultType="Integer" order="AFTER"> select LAST_INSERT_ID() </selectKey> insert into user (username,birthday,address,sex) values (#{username},#{birthday},#{address},#{sex}) </insert> <!-- 更新 --> <update id="updateUserById" parameterType="User"> update user set username = #{username},sex = #{sex},birthday = #{birthday},address = #{address} where id = #{id} </update> <!-- 删除 --> <delete id="deleteUserById" parameterType="Integer"> delete from user where id = #{vvvvv} </delete> </mapper>
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Java--反射(框架设计的灵魂)案例 2020-06-11
- MyBatis中的$和#,用不好,准备走人! 2020-06-11
- Java--反射(框架设计的灵魂) 2020-06-11
- SpringBoot 2.3 整合最新版 ShardingJdbc + Druid + MyBatis 2020-06-11
- 与JAVA集合相遇 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash