zoukankan      html  css  js  c++  java
  • 二、两种方法查询数据库

    一、获取session,session直接调用mapper配置文件里写的方法。

    二、获取session,session再通过生成mapper接口实现类来查询。这里需要注意的就是,如mapper/StudentMapper.java接口需要同xmlmaper文件的namespace一致。

     1 public class TestXmlBuild1 {
     2     public static SqlSessionFactory sqlMapper = null;
     3 
     4     @Before
     5     public void setUpClass() throws IOException {
     6         String resource = "config/Configuration1.xml";
     7         Reader reader = Resources.getResourceAsReader(resource);
     8         sqlMapper = new SqlSessionFactoryBuilder().build(reader);
     9     }
    10 
    11     @Test
    12     public void test1() {
    13         SqlSession session = sqlMapper.openSession();
    14         //第一种方法,直接调用,名字要和<mapper namespace="mapper.StudentMapper">匹配
    15         try {
    16             Student student = (Student) session.selectOne("mapper.StudentMapper.get", "ttt");
    17             System.out.println(student);
    18         } finally {
    19             session.close();
    20         }
    21     }
    22     
    23     @Test
    24     public void test2() {
    25         SqlSession session = sqlMapper.openSession();
    26         //第二种方法,通过接口,mapper名字需要和<mapper namespace="mapper.StudentMapper">匹配
    27         try {
    28             StudentMapper studentMapper = session.getMapper(StudentMapper.class);
    29             Student student = studentMapper.get("ttt"); 
    30             System.out.println(student);
    31         } finally {
    32             session.close();
    33         }
    34     }
    35 }

    StudentMapper.xml

     1 <?xml version="1.0" encoding="UTF-8"?> 
     2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
     3     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
     4 <mapper namespace="mapper.StudentMapper">
     5     <resultMap type="Student" id="StudentResultMap">
     6         <id property="id" column="id" />
     7         <result property="name" column="name" />
     8         <result property="password" column="password" />
     9         <result property="sex" column="sex" />
    10         <result property="college" column="college" />
    11         <collection property="scores" column="sid" javaType="ArrayList" ofType="Score" resultMap="Score.ScoreResultMap" />
    12     </resultMap>
    13     <select id="get" parameterType="String" resultMap="StudentResultMap">
    14         SELECT * FROM sc_student st LEFT OUTER JOIN sc_score sc ON st.id=sc.sid WHERE st.id=#{id}
    15     </select>
    16     <insert id="insert" parameterType="Student">
    17         insert into sc_student (id,name,password,sex,college) values
    18         (#{id},#{name},#{password},#{sex},#{college})
    19     </insert>
    20     <update id="update" parameterType="Student">
    21         UPDATE sc_student SET id=#{id},name=#{name},password=#{password},
    22         sex=#{sex},college=#{college} WHERE id=#{id}
    23     </update>
    24     <delete id="delete" parameterType="Student">
    25         DELETE FROM sc_student WHERE id = #{id}
    26     </delete>
    27 </mapper>

    mapper/StudentMapper.java

    1 package mapper;
    2 
    3 import entity.Student;
    4 
    5 public interface StudentMapper {
    6     //@Select("SELECT * FROM blog WHERE id = #{id}") //不写mapper可以用这种方式代替,没试过。
    7     public Student get(String id); 
    8 }

    Configuration.xml

     1 <?xml version="1.0" encoding="UTF-8" ?> 
     2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 
     3 "http://mybatis.org/dtd/mybatis-3-config.dtd">
     4 <configuration>
     5     <!-- 用于建立会话的变量,可以从文件中读取 -->
     6     <properties resource="config/config.properties">
     7         <property name="driver" value="com.mysql.jdbc.Driver" />
     8         <property name="url" value="jdbc:mysql://localhost:3306/test" />
     9         <property name="username" value="root" />
    10         <property name="password" value="asd123" />
    11     </properties>
    12 
    13     <!-- 可以不设置 -->
    14     <!-- <setting></setting> -->
    15     
    16     <typeAliases>
    17         <typeAlias alias="Student" type="entity.Student"></typeAlias>
    18         <typeAlias alias="Score" type="entity.Score"></typeAlias>
    19     </typeAliases>
    20     <environments default="development">
    21         <environment id="development">
    22             <transactionManager type="JDBC" />
    23             <dataSource type="POOLED">
    24                 <property name="driver" value="${driver}" />
    25                 <property name="url" value="${url}" />
    26                 <property name="username" value="${username}" />
    27                 <property name="password" value="${password}" />
    28             </dataSource>
    29         </environment>
    30     </environments>
    31     <mappers>
    32         <mapper resource="mapper/StudentMapper.xml" />
    33         <mapper resource="entity/ScoreMapper.xml" />
    34     </mappers>
    35 </configuration> 
  • 相关阅读:
    数据库字段太多,批量快速建立实体类方法(适合大量字段建立实体类)
    SQL service 中的 ”输入SQL命令窗口“ 打开了 “属性界面” 回到 ”输入SQL命令窗口“
    计算机软件编程英语词汇集锦
    编程常用英语词汇
    svn上传和下载项目
    当启动tomcat时出现tomcat setting should be set in tomcat preference page
    Implicit super constructor Object() is undefined for default constructor. Must define an explicit constructor
    eclipse中选中一个单词 其他相同的也被选中 怎么设置
    Spring Boot的@SpringBootApplication无法引入的问题
    最全的SpringCloud视频教程
  • 原文地址:https://www.cnblogs.com/xunol/p/3238773.html
Copyright © 2011-2022 走看看