一、两种方式初始化Mybatis
一、xml
Configuration.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>
<!-- 用于建立会话的变量,可以从文件中读取 -->
<properties resource="config/config.properties">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="asd123" />
</properties> <!-- 可以不设置 -->
<!-- <setting></setting> --> <typeAliases>
<typeAlias alias="Student" type="entity.Student"></typeAlias>
<typeAlias alias="Score" type="entity.Score"></typeAlias>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/StudentMapper.xml" />
<mapper resource="entity/ScoreMapper.xml" />
</mappers>
</configuration>
StudentMapper.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">
<mapper namespace="mapper.StudentMapper"> <resultMap type="Student" id="StudentResultMap">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="password" column="password" />
<result property="sex" column="sex" />
<result property="college" column="college" />
<collection property="scores" column="sid" javaType="ArrayList" ofType="Score" resultMap="Score.ScoreResultMap" />
</resultMap> <select id="get" parameterType="String" resultMap="StudentResultMap">
SELECT * FROM sc_student st LEFT OUTER JOIN sc_score sc ON st.id=sc.sid WHERE st.id=#{id}
</select>
<insert id="insert" parameterType="Student">
insert into sc_student (id,name,password,sex,college) values
(#{id},#{name},#{password},#{sex},#{college})
</insert>
<update id="update" parameterType="Student">
UPDATE sc_student SET id=#{id},name=#{name},password=#{password},
sex=#{sex},college=#{college} WHERE id=#{id}
</update>
<delete id="delete" parameterType="Student">
DELETE FROM sc_student WHERE id = #{id}
</delete>
</mapper>
package test; import java.io.IOException;
import java.io.Reader; import mapper.StudentMapper; 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.Before;
import org.junit.BeforeClass;
import org.junit.Test; import entity.Student; public class TestXmlBuild1 {
public static SqlSessionFactory sqlMapper = null; @Before
public void setUpClass() throws IOException {
String resource = "config/Configuration1.xml";
Reader reader = Resources.getResourceAsReader(resource);
sqlMapper = new SqlSessionFactoryBuilder().build(reader);
} @Test
public void test1() {
SqlSession session = sqlMapper.openSession();
//第一种方法,直接调用,名字要和<mapper namespace="mapper.StudentMapper">匹配
try {
Student student = (Student) session.selectOne("mapper.StudentMapper.get", "ttt");
System.out.println(student);
} finally {
session.close();
}
} @Test
public void test2() {
SqlSession session = sqlMapper.openSession();
//第二种方法,通过接口,mapper名字需要和<mapper namespace="mapper.StudentMapper">匹配
try {
StudentMapper studentMapper = session.getMapper(StudentMapper.class);
Student student = studentMapper.get("ttt123");
System.out.println(student);
} finally {
session.close();
}
}
}
二、不使用XML构建SqlSessionFactory
没用到,就不解释了。
DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
TransactionFactory transactionFactory = new
JdbcTransactionFactory();
Environment environment =
new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(BlogMapper.class);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(configuration);
一、两种方式初始化Mybatis的更多相关文章
- easyUI 初始化的两种方式
easyUI 初始化的两种方式: class方式和js方式: <!DOCTYPE html> <html lang="en"> <head> & ...
- 使用GoldenGate初始化的两种方式
在使用OGG开始增量数据的实时复制之前,一般需要对当前的存量数据进行初始化,如果是同构数据库,则可以使用数据库自带的工具完成,比如Oracle DB中的rman, expdp/impdp等. 其实og ...
- MyBatis开发Dao层的两种方式(原始Dao层开发)
本文将介绍使用框架mybatis开发原始Dao层来对一个对数据库进行增删改查的案例. Mapper动态代理开发Dao层请阅读我的下一篇博客:MyBatis开发Dao层的两种方式(Mapper动态代理方 ...
- MyBatis开发Dao层的两种方式(Mapper动态代理方式)
MyBatis开发原始Dao层请阅读我的上一篇博客:MyBatis开发Dao层的两种方式(原始Dao层开发) 接上一篇博客继续介绍MyBatis开发Dao层的第二种方式:Mapper动态代理方式 Ma ...
- springboot整合mybatis的两种方式
https://blog.csdn.net/qq_32719003/article/details/72123917 springboot通过java bean集成通用mapper的两种方式 前言:公 ...
- MyBatis配置数据源的两种方式
---------------------siwuxie095 MyBatis 配置数据源的两种方式 1.配置方 ...
- 设置Mybatis打印调试sql的两种方式
http://blog.csdn.net/gao36951/article/details/53641432 ********************************************* ...
- mybatis批量保存的两种方式(高效插入)
知识点:mybatis中,批量保存的两种方式 1.使用mybatis foreach标签 2.mybatis ExecutorType.BATCH 参考博客:https://www.jb51.net/ ...
- JavaWeb应用中初始化Log4j的两种方式
本文主要介绍了普通JavaWeb应用(基于Tomcat)中初始化Log4j的两种方式: 1.通过增加 InitServlet ,设置令其自启动来初始化 Log4j . 2.通过监听器 ServletC ...
随机推荐
- 【VB6 学习文档管理系统源码】
VB6写的一款笔记软件的源码,里面包含有很多窗体控件的使用技巧,比如MSHFlexgrid表格.TreeView的动态加载.Ado的增删改查等. 本软件提供对日常生活.工作中的学习笔记.图文并茂存储以 ...
- scrapy使用爬取多个页面
scrapy是个好玩的爬虫框架,基本用法就是:输入起始的一堆url,让爬虫去get这些网页,然后parse页面,获取自己喜欢的东西.. 用上去有django的感觉,有settings,有field.还 ...
- web2.0最全的国外API应用集合
web2.0最全的国外API应用集合 原文地址:http://www.buguat.com/post/98.html 2.0时代,越来越多的API被大家广泛应用,如果你还不了解API是何物,请看这里的 ...
- xcopy总是询问是文件名还是目录名
我需要运行类似xcopy /y a.xml .\pics\b.xml很多次,但xcopy总是问我“文件名还是目录名” 可以这样通过管道来做echo f | xcopy /y a.xml .\pics\ ...
- C# 操作XML文件,用XML文件保存信息
using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.IO ...
- 旧版Xcode下载地址
怕忘记了,做个记号 https://developer.apple.com/downloads/
- sql server 表空间
在SqlServer2005中,建表时是默认把所有的表都保存在PRIMARY默认表空间中的.当数据库中表很多,并且数据量很大时,会导致数据库性能严重下降,有必要将一些大的表放到不同的表空间中去.主要的 ...
- iOS -一些常用的方法
1.获取本地的语言 + (NSString *)getLocalLanguage { NSString *language = [[[NSUserDefaults standardUserDefaul ...
- 关于回波损耗 和 驻波比的摘要 Return Loss and VSWR
关于回波损耗 和 驻波比的摘要 以下摘自:http://www.soontai.com/cal_rtvswr.html RL = 20log((VSWR+1) / (VSWR-1)) VSWR = ( ...
- android中viewPager+fragment实现的屏幕左右切换(进阶篇)
Fragment支持在不同的Activity中使用并且可以处理自己的输入事件以及生命周期方法等.可以看做是一个子Activity. 先看一下布局: 1 <LinearLayout xmlns:a ...