(读书笔记)mybatis (三)-----environments详解
1.导入jar包mybatis-3.2.7.jar,mysql-commector-lava-5.1.6-bin.jar
2.创建mybatis-config.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>
<!-- 配置实体类的别名,配置实体类别名的目的是为了在引用实体类时可以使用实体类的别名来代替实体类,达到简写的目的 -->
<typeAliases>
<!-- 为实体类me.gacl.domain.User配置一个别名_User -->
<!-- <typeAlias type="org.bean.KaoTi" alias="_KaoTi"/> -->
<!-- 为org.bean包下的所有实体类配置别名,MyBatis默认的设置别名的方式就是去除类所在的包后的简单的类名
比如org.bean.KaoTi这个实体类的别名就会被设置成KaoTi
-->
<package name="org.bean"/>
</typeAliases>
<!-- 引用db.properties配置文件 -->
<properties resource="db.properties"/>
<!--
development : 开发模式
work : 工作模式
-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<!-- value属性值引用db.properties配置文件中配置的值 -->
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${name}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<!-- 配置mapper文件 -->
<mappers>
<mapper resource="org/mapper/kaoTiMapper.xml"/>
</mappers>
</configuration>
3.创建db.properties文件
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://188.178.170.171:3306/
name=root
password=root
4.创建javabean (get,set方法)此处省略,
5.创建mapper.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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀)
-->
<mapper namespace="org.mapper.kaoTiMapper">
<!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getKaoTiById,id属性值必须是唯一的,不能够重复
使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型
resultType="org.bean.KaoTi"就表示将查询结果封装成一个User类的对象返回
User类就是users表所对应的实体类
-->
<!-- 根据id查询得到一个KaoTi对象 -->
<select id="getKaoTiById" parameterType="int" resultType="org.bean.KaoTi">
select * from kaoti where kaoTi_id = #{kaoTi_id}
</select>
<select id="getKaoTiById1" parameterType="int" resultMap="allKaoti">
select * from kaoti where kaoTi_id = #{kaoTi_id}
</select>
<select id="selectAll" resultMap="allKaoti">
select kaoti_id,title from kaoti
</select>
<resultMap type="org.bean.KaoTi" id="allKaoti">
<id property="kaoTi_id" column="kaoTi_id"/>
<result property="title" column="title"/>
</resultMap>
<!-- 增加内容(insert) -->
<insert id="addKaoTi" parameterType="org.bean.KaoTi">
insert into kaoti(title) values(#{title})
</insert>
<select id="getOne" resultMap="allKaoti">
select kaoti_id,title from kaoti order by kaoTi_id desc limit 1
</select>
<!-- 修改内容(Update) -->
<update id="updateById" parameterType="org.bean.KaoTi">
update users set title=#{title} where kaoTi_id=#{kaoTi_id}
</update>
<!-- 删除内容 -->
<delete id="deleteById" parameterType="int">
delete from kaoti where kaoTi_id = #{kaoTi_id}
</delete>
</mapper>
6.创建test.java
package org.test;
import java.io.IOException;
import java.io.InputStream;
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;
import org.bean.KaoTi;
import org.junit.Test;
public class Test1 {
public static SqlSession getSqlSession(){
String resource = "configuration.xml";
InputStream inputStream;
try {
inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
return session;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Test
public void getKaoTiById1(){
SqlSession session = Test1.getSqlSession();
/**
* 映射sql的标识字符串,
* me.gacl.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值,
* getUser是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL
*/
String statement = "org.mapper.kaoTiMapper.getKaoTiById1";//映射sql的标识字符串
System.out.println(statement);
//执行查询返回一个唯一user对象的sql
KaoTi user = session.selectOne(statement, 355);
System.out.println(user);
// String statement = "org.mapper.kaoTiMapper.selectAll";//映射sql的标识字符串
// System.out.println(statement);
// //执行查询返回一个唯一user对象的sql
// List<KaoTi> list = session.selectList(statement);
// for(KaoTi k : list){
// System.out.println(k);
// }
}
@Test
public void getKaoTiById(){
SqlSession session = Test1.getSqlSession();
String statement = "org.mapper.kaoTiMapper.getKaoTiById";//映射sql的标识字符串
System.out.println(statement);
//执行查询返回一个唯一user对象的sql
KaoTi user = session.selectOne(statement, 355);
System.out.println(user);
}
@Test
public void addKaoTi(){
SqlSession session = Test1.getSqlSession();
String statement = "org.mapper.kaoTiMapper.addKaoTi";//映射sql的标识字符串
System.out.println(statement);
KaoTi kt = new KaoTi();
kt.setTitle("jsp有哪些内置对象?作用分别是什么?(至少三个)");
int i = session.insert(statement, kt);
System.out.println(i);
session.commit();
session.close();
}
// @Test
// public void updateById(){
// SqlSession session = Test1.getSqlSession();
// String statement = "org.mapper.kaoTiMapper.updateById";//映射sql的标识字符串
// System.out.println(statement);
// KaoTi kt = new KaoTi();
// kt.setKaoTi_id(609);
// kt.setTitle("jsp有哪些内置对象?作用分别是什么?(至少4个)");
//
// int i = session.update(statement, kt);
// System.out.println(i);
// session.close();
// }
@Test
public void getOne(){
SqlSession session = Test1.getSqlSession();
String statement = "org.mapper.kaoTiMapper.getOne";//映射sql的标识字符串
System.out.println(statement);
//执行查询返回一个唯一user对象的sql
KaoTi user = session.selectOne(statement);
System.out.println(user);
}
@Test
public void deleteById(){
SqlSession session = Test1.getSqlSession();
String statement = "org.mapper.kaoTiMapper.deleteById";//映射sql的标识字符串
System.out.println(statement);
int i = session.delete(statement, 607);
session.commit();
session.close();
System.out.println(i);
}
}