Mybatis的搭建
来源:互联网 发布:大数据征信查询app 编辑:程序博客网 时间:2024/05/19 19:42
1.持久化 持久化,就是内存数据和硬盘数据状态的转换 2.ORM思想Object Relation Mapping 对象关系映射 3.MyBatis入门案例 3.1导入jar包 依赖 <!--MySQL配置--> <dependency> <groupId>MySQL</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.8</version> </dependency> <!--MyBatis核心jar包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.2</version> </dependency> 3.2书写大配置 <?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/y2165"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="org/mybatis/example/BlogMapper.xml"/> </mappers></configuration> 3.3实体类(实体类的列必须和数据库的列一致) public class StudentInfo { private Integer stuId; private String stuName; private Integer stuAge; private Date stuDate; } 小配置:<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="bdqn.dao."> <select id="select" resultType="bdqn.entity.Department"> select * from department </select> <select id="selectid" resultType="Department"> select * from department where id=#{id} </select></mapper>
<?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="cn.happy.dao.IStudentInfoDAO"> <select id="findAll" resultType="cn.happy.entity.StudentInfo"> select * from studentinfo </select></mapper> 两个注意事项: 1.你得更新POM.xml文件中build节点 <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>
3.5测试类 public class DepartmentTest { @Test public void show(){ //查询列表 /* try { InputStream stream=Resources.getResourceAsStream("mybatis-config.xml.tld"); SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(stream); SqlSession session=sqlSessionFactory.openSession(); List<Department> list=session.selectList("select"); for (Department item:list ) { System.out.println(item.getName()); } } catch (IOException e) { e.printStackTrace(); }*/ //根据id查部门 try { InputStream stream=Resources.getResourceAsStream("mybatis-config.xml.tld"); SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(stream); SqlSession session=sqlSessionFactory.openSession(); Department one=session.selectOne("selectid",1); System.out.println(one.getName()); } catch (IOException e) { e.printStackTrace(); } }} 结论: 1. 注意小配置的命名空间的名称 4.别名的使用(加在大配置里) <typeAliases> <!--<typeAlias type="cn.happy.entity.StudentInfo" alias="StudentInfo"></typeAlias>--> <!--将该包中的简单类型 StudentInfo作为类的别名--> <package name="cn.happy.entity"></package> </typeAliases> 小配置<select id="select" resultType="bdqn.entity.Department">改为<select id="select" resultType="Department">5.getMapper() 动态代理数据 class<T> 类型的类型 is = Resources.getResourceAsStream(path); SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is); SqlSession session=factory.openSession(); IStudentInfoDAO dao = session.getMapper(IStudentInfoDAO.class); StudentInfo info = dao.getStudentById(3); System.out.println(info.getStuName());
阅读全文
0 0
- 自己的Mybatis搭建
- MyBatis的环境搭建
- 简单的Mybatis搭建
- Mybatis的搭建
- Mybatis的搭建
- 初始mybatis (mybatis开发环境的搭建)
- spring+mybatis+restful的搭建
- MyBatis的下载与搭建
- Mybatis开发环境的搭建
- Spring Boot+Mybatis的搭建
- MyBatis开发环境的搭建
- 简单mybatis环境搭建 我的第一个mybatis工程
- Spring,SpringMVC,Mybatis (SSM)框架的搭建
- Mybatis的原理、搭建与使用
- spring+springmvc+mybatis框架的搭建
- Maven+SpringMVC+MyBatis的环境搭建(一)
- Maven+SpringMVC+MyBatis的环境搭建(二)
- Maven+Spring+SpringMVC+MyBatis框架的搭建
- Prime Ring Problem
- Android 报 Unable to start service Intent { } U=0: not found错误
- Eclipse新建类的时候如何自动添加注释(作者,时间的信息)
- 九个Console命令,让 JS 调试更简单
- java类执行顺序
- Mybatis的搭建
- 查缺补漏--php(自动转为false的情况)
- python 中文汉字钱转成阿拉伯数字钱
- java http下载文件
- Navicat Premium---连接云数据库,每次都要输入密码的解决办法
- JavaScript Date 对象
- 【其他】3. Office2013激活工具(Microsoft Toolkit) V2.5.1
- 读《图形、色彩、文字、编排、网格设计参考书》
- IO流