初始MyBatis

xiaoxiao2021-02-27  182

一、   什么是持久化

答:持久化就是内存数据和硬盘数据状态的转换

二、ORM思想

  ORM表面意思是:  Object  Relation Mapping 对象关系映射

三、 MyBatis入门案例

                第一步:导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>

                第 二步:书写大配置

                  <?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>

----------------------配置中   property   字段为数据库相关  看个人数据库进行修改

      第三步:小配置

               <?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>

---------------------------其中的mapper字段后为DAO类   路径看个人情况修改        Select中数据库代码为想要的条件可以自行修改

  两个注意事项:   1.你得更新POM.xml文件中build节点   <build>     <resources>       <resource>         <directory>src/main/java</directory>         <includes>           <include>**/*.xml</include>         </includes>       </resource>     </resources>   </build>      2.你得在大配置中关联小配置文件      <mappers>         <mapper resource="cn/happy/dao/IStudentInfoDAO.xml"/>     </mappers>

四、别名的使用     <typeAliases>         <!--<typeAlias type="cn.happy.entity.StudentInfo" alias="StudentInfo"></typeAlias>-->         <!--将该包中的简单类型 StudentInfo作为类的别名-->         <package name="cn.happy.entity"></package>     </typeAliases>

总体而言就是 在大配置中写入以上代码 路径为小配置中的公共路径   比如:cn.happy.entity  在小配置中直接写入自己的类   共同用一个包  就是别名

五、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());

上面为类的简单使用

        

转载请注明原文地址: https://www.6miu.com/read-15135.html

最新回复(0)