①就是配置依赖
⑤就是mybatis总配置文件
mybatis.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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/emp"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="EmpMapper.xml"/>
</mappers>
</configuration>
表映射.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="empMapper" >
<delete id="delete" parameterType="lg.entity.Emp">
delete from emp where id = #{id};
</delete>
<update id="update" parameterType="lg.entity.Emp">
update emp set name = #{name},salary =#{salary} where id = #{id};
</update>
<select id="findAll" resultType="lg.entity.Emp">
select * from emp;
</select>
<insert id="save" parameterType="lg.entity.Emp">
insert into emp values(#{id},#{name},#{salary})
</insert>
</mapper>
测试类
import lg.entity.Emp;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MyBatisTest {
@Test
public void test1() throws IOException {
//获得核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis.xml");
//获得session工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得session对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行操作
List<Emp> empList = sqlSession.selectList("empMapper.findAll");
System.out.println(empList);
sqlSession.close();
}
@Test
public void test2() throws IOException {
Emp emp = new Emp();
// emp.setId(9);
emp.setName("ooo");
emp.setSalary(1234);
//获得核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis.xml");
//获得session工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得session对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行操作
sqlSession.insert("empMapper.save",emp);
//mybatis执行更新操作都需要手动提交事务
sqlSession.commit();
sqlSession.close();
}
@Test
public void test3() throws IOException {
Emp emp = new Emp();
emp.setId(3);
emp.setName("lulalala");
emp.setSalary(99999);
//获得核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis.xml");
//获得session工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得session对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行操作
sqlSession.insert("empMapper.update",emp);
//mybatis执行更新操作都需要手动提交事务
sqlSession.commit();
sqlSession.close();
}
@Test
public void test4() throws IOException {
//获得核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis.xml");
//获得session工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得session对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行操作
sqlSession.delete("empMapper.delete",3);
//mybatis执行更新操作都需要手动提交事务
sqlSession.commit();
sqlSession.close();
}
}
黄标
1、resources目录下想要创建父子结构的目录要用/不能用.
2、在使用框架的情况下,实体类都生成一个无参的构造器!!!