mybatis笔记-3、入门案例

一、conf.xml

--------------conf.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"><!--导入了dtd约束-->
<configuration>
	<environments default="development">
		<environment id="development">
		<transactionManager type="JDBC"/>
			<dataSource type="POOLED">
				<!-- 配置数据库信息 -->
			<property name="driver" value="oracle.jdbc.OracleDriver"/>
			<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:ORCL"/>
			<property name="username" value="scott"/>
			<property name="password" value="tiger"/>
		</dataSource>
		</environment>
	</environments>
    
	<mappers>
		<!-- 加载映射文件 -->
		<mapper resource="org/lanqiao/entity/personMapper.xml"/>
	</mappers>
</configuration>
environment:

可以多个。通过environments的default值 和 environment的 id 来指定 MyBatis运行时的数据库环境-。开发环境(自己的计算机)。,

<configuration>
	<environments default="development">
		<environment id="development">


---------------------------------
Reader reader = Resources.getResourceAsReader("conf.xml") ;

SqlSessionFactory sessionFacotry = 
            new SqlSessionFactoryBuilder().build(reader,"test") ;//拿第二个参数手工指定开发环境。
事务提交方式:
  • 事务提交方式:
    • JDBC:利用JDBC方式处理事务(commit rollback close)
    • MANAGED:将事务交由 其他组件去托管(spring ,jobss),默认 会关闭连接。
    • 在里面写一句可以将MANAGED的默认关闭取消
dataSource
  • UNPOOLED:传统的JDBC模式(不推荐。每次访问数据库,均需要 打开、关闭等数据库操作,但是 打开、关闭数据库是比较消耗性能的)
  • POOLED:使用数据库连接池(常用)
  • JNDI:从tomcat中获取一个内置的数据库连接池 (数据库连接池-数据源 )
<dataSource type="POOLED">
    <!-- 配置数据库信息 -->
    <property name="driver" value="oracle.jdbc.OracleDriver"/>
    <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:ORCL"/>
    <property name="username" value="scott"/>
    <property name="password" value="tiger"/>
</dataSource>

怎么导入提示:有网自动提示,没网的时候想要查看提示可以:点击链接下载后,Preference,XML Catalog ,Add,传入文件和conf.xml中PUBLIC信息"-//mybatis.org//DTD Config 3.0//EN"

如何把链接信息写到别地方?看properties文件
<?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值 和  environment的 id 来指定 MyBatis运行时的数据库环境-->
	<environments default="development">
		<!-- 开发环境(自己的计算机) -->
		<environment id="development">
            	<!-- 事务提交方式:
				JDBC:利用JDBC方式处理事务(commit  rollback  close)
				MANAGED:将事务交由 其他组件去托管(spring ,jobss),默认 会关闭连接。

				<transactionManager type="MANAGED"/>
				<property name="closeConnection" value="false"/>
				-->
				<transactionManager type="JDBC" />
            
				<!-- 数据源类型:(数据库连接池)
						UNPOOLED:传统的JDBC模式(每次访问数据库,均需要 打开、关闭等数据库操作,但是 打开、关闭数据库是比较消耗性能的)
						POOLED:使用数据库连接池
						JNDI:从tomcat中获取一个内置的数据库连接池 (数据库连接池-数据源  )
				 -->
				<dataSource type="POOLED">
					<!-- 配置数据库信息 -->
					<property name="driver" value="oracle.jdbc.OracleDriver"/>
					<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:ORCL"/>
					<property name="username" value="scott"/>
					<property name="password" value="tiger"/>
				</dataSource>
		</environment>
		
		<!-- 真正的项目应该在   发布的那台计算机上运行   Server -->
		<environment id="shishi">
			<transactionManager type="JDBC"/>
				<dataSource type="POOLED">
				<!-- 配置数据库信息 -->
				<property name="driver" value="oracle.jdbc.OracleDriver"/>
				<property name="url" value="jdbc:oracle:thin:@192.168.1.183:1521:ORCL"/>
				<property name="username" value="scott"/>
				<property name="password" value="tiger"/>
			</dataSource>
		</environment>
		
		
		<!--  -->
		<environment id="test">
			<transactionManager type="JDBC"/>
				<dataSource type="POOLED">
				<!-- 配置数据库信息 -->
				<property name="driver" value="oracle.jdbc.OracleDriver"/>
				<property name="url" value="jdbc:oracle:thin:@192.168.1.111:1521:ORCL"/>
				<property name="username" value="scott"/>
				<property name="password" value="tiger"/>
			</dataSource>
		</environment>
	</environments>
	
	
	
	<mappers>
		<!-- 加载映射文件 -->
		<mapper resource="org/lanqiao/entity/studentMapper.xml"/>
	</mappers>
</configuration>

代码中标签相关的文件:动态代理、全局函数、properties、别名setting、

二、java类文件

package org.lanqiao.entity;

public class Student {
	private int id;
	private String name;
	private int age ;
	
	public Student() {
	}
	public Student(int id, String name, int age) {
		this.id = id;
		this.name = name;
		this.age = age;
	}
	+GET SET 省略....
    	@Override
	public String toString() {
		return this.id+","+this.name+","+this.age ;
	}
}

三、类mapper.xml

用mapper文件将类和数据库关联起来。

  • id:

输入参数parameterType 和 输出参数resultType ,在形式上都只能有一个

  • resultType:返回值类型,只能是一个值,但可以是一个复合类型。值得注意的是,如果返回值是一个符合类型的数组,还是只写全类名,没有[]。只在java文件中接收时才写List<类名> list1用来接收;

输出参数: 如果返回值类型是一个 对象(如Student),则无论返回一个、还是多个结果,resultType都写成org.lanqiao.entity.Student
即 resultType=“org.lanqiao.entity.Student”

  • parameterType:输入参数类型,只能是一个值,但可以是一个复合类型

如果输入参数 :是简单类型(8个基本类型+String) 是可以使用任何占位符,#{xxxx}
如果是对象类型,则必须是对象的属性 #{属性名}

中间内容:

  • sql语句可以传入值,当parameterType为基本类型时,{}内的名称任意;当parameterType不为简单类型(8个基本类型+string)时,{}内名称不能随意更改
  • 占位符placeholder用#{id}表示
<?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">

<!-- namespace:该mapper.xml映射文件的 唯一标识 -->
<mapper namespace="org.lanqiao.entity.studentMapper">
	<!--  后续通过  namespace.id-->
	<!--parameterType:输入参数的类型
	resultType:查询返回结果值的类型  ,返回类型  -->
	<select id="queryStudentByStuno" parameterType="int"  resultType="org.lanqiao.entity.Student" >
			select * from student where stuno = #{stuno}
	</select>
	
	
	<insert id="addStudent" parameterType="org.lanqiao.entity.Student" >
		insert into student(stuno,stuname,stuage,graname) values(#{stuNo},#{stuName},#{stuAge},#{graName}  ) 
	</insert>
	
	<delete id="deleteStudentByStuno"  parameterType="int">
		delete from student where stuno = #{stuno} 
	</delete>
	
	<update id="updateStudentByStuno" parameterType="org.lanqiao.entity.Student" >
		update student set stuname=#{stuName} ,stuage=#{stuAge},graname=#{graName} where stuno=#{stuNo} 
	</update>
	
	<select id="queryAllStudents"  resultType="org.lanqiao.entity.Student" >
		select * from student 
	</select>

	
</mapper>

namespace:xml文件名。该mapper.xml映射文件的 唯一标识

四、测试java程序

package org.lanqiao.entity;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class Test {
	//查询单个学生
	public static void queryStudentByStuno() throws IOException {
		//Connection -  SqlSession操作MyBatis
				//conf.xml - > reader
				Reader reader = Resources.getResourceAsReader("conf.xml") ;
				//reader  ->SqlSession
				
				//可以通过build的第二参数 指定数据库环境
				SqlSessionFactory sessionFacotry = new SqlSessionFactoryBuilder().build(reader,"development") ;
				SqlSession session = sessionFacotry.openSession() ;
				String statement = "org.lanqiao.entity.studentMapper.queryStudentByStuno";
				Student student = session.selectOne(statement,1) ;
				System.out.println(student);
				session.close();
	}
	
	  //查询全部学生
 		public static void queryAllStudents() throws IOException {
			//Connection -  SqlSession操作MyBatis
					//conf.xml - > reader
					Reader reader = Resources.getResourceAsReader("conf.xml") ;
					//reader  ->SqlSession
					//可以通过build的第二参数 指定数据库环境
					SqlSessionFactory sessionFacotry = new SqlSessionFactoryBuilder().build(reader,"development") ;
					SqlSession session = sessionFacotry.openSession() ;
					
					
					
					String statement = "org.lanqiao.entity.studentMapper."+"queryAllStudents";
					List<Student> students = session.selectList(statement) ;
					System.out.println(students);
					session.close();
		}
		
 		
 		 //增加学生
 		public static void addStudent() throws IOException {
 			//Connection -  SqlSession操作MyBatis
					//conf.xml - > reader
					Reader reader = Resources.getResourceAsReader("conf.xml") ;
					//reader  ->SqlSession
					//可以通过build的第二参数 指定数据库环境
					SqlSessionFactory sessionFacotry = new SqlSessionFactoryBuilder().build(reader,"development") ;
					SqlSession session = sessionFacotry.openSession() ;
					
					String statement = "org.lanqiao.entity.studentMapper."+"addStudent";
					Student student = new Student(3,"ww",25,"s1");
					
					int count = session.insert(statement, student );//statement:指定执行的SQL    student:sql中需要的参数 ( ? ? ? )
					session.commit(); //提交事务
					
					System.out.println("增加"+count+"个学生");
					session.close();
		}
 		
 		
 		 //删除学生
 		public static void delteStudentByStuno() throws IOException {
 			//Connection -  SqlSession操作MyBatis
					//conf.xml - > reader
					Reader reader = Resources.getResourceAsReader("conf.xml") ;
					//reader  ->SqlSession
					//可以通过build的第二参数 指定数据库环境
					SqlSessionFactory sessionFacotry = new SqlSessionFactoryBuilder().build(reader,"development") ;
					SqlSession session = sessionFacotry.openSession() ;
					
					String statement = "org.lanqiao.entity.studentMapper."+"deleteStudentByStuno";
					
					int count = session.delete(statement,3) ;
					
					session.commit(); //提交事务
					
					System.out.println("删除"+count+"个学生");
					session.close();
		}
	
 		 //修改学生
 		public static void updateStudentByStuno() throws IOException {
 			//Connection -  SqlSession操作MyBatis
					//conf.xml - > reader
					Reader reader = Resources.getResourceAsReader("conf.xml") ;
					//reader  ->SqlSession
					//可以通过build的第二参数 指定数据库环境
					SqlSessionFactory sessionFacotry = new SqlSessionFactoryBuilder().build(reader,"development") ;
					SqlSession session = sessionFacotry.openSession() ;
					
					String statement = "org.lanqiao.entity.studentMapper."+"updateStudentByStuno";
					//修改的参数
					Student student = new Student();
					//修改哪个人,where stuno =2 
					student.setStuNo(2);
					//修改成什么样子?
					student.setStuName("lxs");
					student.setStuAge(44);
					student.setGraName("s2");
					//执行
					int count = session.update(statement,student) ;
					
					session.commit(); //提交事务
					
					System.out.println("修改"+count+"个学生");
					session.close();
		}
	
	
	public static void main(String[] args) throws IOException {
		queryAllStudents();
//		addStudent() ;
//		delteStudentByStuno();
		updateStudentByStuno();
		queryAllStudents();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值