标签:encoding dev color tac nsa nfa dtd config source
<?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">
<!-- MyBatis的全局配置文件 -->
<configuration >
<environments default="develop">
<environment id="develop" >
<transactionManager type="JDBC">
</transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///yonghedb?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="EmpMapper.xml"/>
</mappers>
</configuration>
<?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一般指定为当前文件的所在包路径+文件名(将来是接口名)
在程序中通过[ namespace + id ]定位到执行哪一条SQL语句
-->
<mapper namespace="EmpMapper">
<select id="findAll" resultType="com.tedu.pojo.Emp">
select * from emp
</select>
<select id="insert">
insert into emp values(null,‘aa‘,‘bb‘,8888)
</select>
<select id="delete">
delete from emp where id=18
</select>
</mapper>
package com.tedu.mybatis;
import java.io.InputStream;
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.junit.Test;
import com.tedu.pojo.Emp;
public class TestMybatis2 {
private static SqlSession session=null;
static{
try {
//1.读取Mybaits核心配置文件(mybatis-config.xml)
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
//2.通过配置信息获取一个 SqlsessionFactory 工厂
SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in);
session = fac.openSession();
} catch (Exception e) {
e.printStackTrace();
}
}
/*练习1: 查询员工信息*/
@Test
public void TestMybatis(){
List<Emp> list = session.selectList("EmpMapper.findAll");
for (Emp emp : list) {
System.out.println(emp);
}
}
/*练习2: 新增员工信息: 刘德华 歌手 888*/
@Test
public void testInsert(){
//执行Sql 语句 返回处理处理后的结果
int rows = session.update("EmpMapper.insert");
// 提交事物
session.commit();
System.out.println("影响行数"+rows);
}
@Test
public void testUpdate(){
//执行Sql 语句 返回处理处理后的结果
int rows = session.update("EmpMapper.update");
// 提交事物
session.commit();
System.out.println("影响行数"+rows);
}
@Test
public void testdelete(){
int rows = session.delete("EmpMapper.delete");
session.commit();
System.out.println("影响行数"+rows);
}
}
标签:encoding dev color tac nsa nfa dtd config source
原文地址:https://www.cnblogs.com/lizhiwei666/p/11722837.html