• 一、两种方式初始化Mybatis


    一、xml

    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> 

    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 
     6 
     7     <resultMap type="Student" id="StudentResultMap">
     8         <id property="id" column="id" />
     9         <result property="name" column="name" />
    10         <result property="password" column="password" />
    11         <result property="sex" column="sex" />
    12         <result property="college" column="college" />
    13         <collection property="scores" column="sid" javaType="ArrayList" ofType="Score" resultMap="Score.ScoreResultMap" />
    14     </resultMap>
    15     
    16     
    17     
    18     <select id="get" parameterType="String" resultMap="StudentResultMap">
    19         SELECT * FROM sc_student st LEFT OUTER JOIN sc_score sc ON st.id=sc.sid WHERE st.id=#{id}
    20     </select>
    21     <insert id="insert" parameterType="Student">
    22         insert into sc_student (id,name,password,sex,college) values
    23         (#{id},#{name},#{password},#{sex},#{college})
    24     </insert>
    25     <update id="update" parameterType="Student">
    26         UPDATE sc_student SET id=#{id},name=#{name},password=#{password},
    27         sex=#{sex},college=#{college} WHERE id=#{id}
    28     </update>
    29     <delete id="delete" parameterType="Student">
    30         DELETE FROM sc_student WHERE id = #{id}
    31     </delete>
    32 </mapper>
     1 package test;
     2 
     3 import java.io.IOException;
     4 import java.io.Reader;
     5 
     6 import mapper.StudentMapper;
     7 
     8 import org.apache.ibatis.io.Resources;
     9 import org.apache.ibatis.session.SqlSession;
    10 import org.apache.ibatis.session.SqlSessionFactory;
    11 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    12 import org.junit.Before;
    13 import org.junit.BeforeClass;
    14 import org.junit.Test;
    15 
    16 import entity.Student;
    17 
    18 public class TestXmlBuild1 {
    19     public static SqlSessionFactory sqlMapper = null;
    20 
    21     @Before
    22     public void setUpClass() throws IOException {
    23         String resource = "config/Configuration1.xml";
    24         Reader reader = Resources.getResourceAsReader(resource);
    25         sqlMapper = new SqlSessionFactoryBuilder().build(reader);
    26     }
    27 
    28     @Test
    29     public void test1() {
    30         SqlSession session = sqlMapper.openSession();
    31         //第一种方法,直接调用,名字要和<mapper namespace="mapper.StudentMapper">匹配
    32         try {
    33             Student student = (Student) session.selectOne("mapper.StudentMapper.get", "ttt");
    34             System.out.println(student);
    35         } finally {
    36             session.close();
    37         }
    38     }
    39     
    40     @Test
    41     public void test2() {
    42         SqlSession session = sqlMapper.openSession();
    43         //第二种方法,通过接口,mapper名字需要和<mapper namespace="mapper.StudentMapper">匹配
    44         try {
    45             StudentMapper studentMapper = session.getMapper(StudentMapper.class);
    46             Student student = studentMapper.get("ttt123"); 
    47             System.out.println(student);
    48         } finally {
    49             session.close();
    50         }
    51     }
    52 }

    二、不使用XML构建SqlSessionFactory 

    没用到,就不解释了。

    1 DataSource dataSource = BlogDataSourceFactory.getBlogDataSource(); 
    2 TransactionFactory transactionFactory = new 
    3 JdbcTransactionFactory(); 
    4 Environment environment = 
    5 new Environment("development", transactionFactory, dataSource); 
    6 Configuration configuration = new Configuration(environment); 
    7 configuration.addMapper(BlogMapper.class); 
    8 SqlSessionFactory sqlSessionFactory = 
    9 new SqlSessionFactoryBuilder().build(configuration);
  • 相关阅读:
    hbase伪分布式安装
    按照《权威指南》的例子求最低温度并且修改默认调度器为FairScheduler
    利用hadoop1.x集群进行探索性实验(四)~(五)【重复格式化NN后,DN不能启动】【控制namenode检查点发生的频率】
    利用hadoop1.x集群进行探索性实验(三)【修改HDFS块大小】
    利用hadoop1.x集群进行探索性实验(二)【模拟namenode崩溃,通过secondary namenode恢复namenode】
    利用hadoop1.x集群进行探索性实验(一)【对WEB监控界面增加安全机制】
    本地编译64位hadoop并进行部署。
    hadoop完全分布式安装
    storm集群和zookeeper集群的部署过程
    查看电脑连接过的wifi密码
  • 原文地址:https://www.cnblogs.com/xunol/p/3238758.html
Copyright © 2020-2023  润新知