mybatis配置------- <?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="jdbc.properties"></properties> <settings> <!-- 设置日志输出底层的代码--> <setting name="logImpl" value="STDOUT_LOGGING"/> <!-- 启动二级缓存--> <setting name="cacheEnabled" value="true"/> <!--启动自动延迟--> <setting name="lazyLoadingEnabled" value="true"/> </settings> <!-- 注册实体类的别名--> <typeAliases> <typeAlias type="" alias=""></typeAlias> </typeAliases> <!--plugins标签,用于配置mybatis插件(分页插件)需配置maven <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.10</version> </dependency> --> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> </plugins> <environments default="mysql"> <environment id="mysql"> <!-- transactionManager标签用于配置数据库管理方式 type="JDBC"可以进行事务的提交和回滚操作 type="MANAGED" 依赖容器完成事务管理,本身不进行事务的提交和回滚操作 --> <transactionManager type="JDBC"/> <!-- dataSource标签配置数据库链接信息--> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <!-- 注册mapper.xml文件--> <mappers> <mapper resource=""/> </mappers> </configuration>
JDBC-------- jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/?characterEncoding=UTF-8 jdbc.username= jdbc.password=
Mybatis------------ public class Mybatis { private static SqlSessionFactory factory=null; static{ String config="mybatis-config.xml"; InputStream in= null; try { in = Resources.getResourceAsStream(config); factory= new SqlSessionFactoryBuilder().build(in); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getsqlSeesion(){ SqlSession sqlSession=null; if (factory!=null) { sqlSession= factory.openSession();/*非自动提交事务*/ } return sqlSession; } }
测试类 @Test//添加用户 public void TestinsertStudent(){ try { //加载mybatis配置文件 InputStream is= Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); //获取SqlSessionFactory SqlSessionFactory factory=builder.build(is); //会话连接 SqlSession sqlSession = factory.openSession(); //获取接口的实现类对象 StudenDao studenDao=sqlSession.getMapper(StudenDao.class); //测试studentDao中的方法 int i=studenDao.insertStudent(new Student(0,"10005","李四","男",21)); System.out.println("添加成功"); sqlSession.commit(); } catch (IOException e) { e.printStackTrace(); } } @Test//删除用户 public void TestDeleStudent(){ SqlSession sqlSession = Mybatis.getsqlSeesion(); StudenDao mapper = sqlSession.getMapper(StudenDao.class); int i = mapper.DeleStudent(1001); if (i>0){ System.out.printf("删除成功"); } sqlSession.commit(); sqlSession.close(); } @Test//更新用户 public void TestupdateStudent(){ SqlSession sqlSession = Mybatis.getsqlSeesion(); StudenDao mapper = sqlSession.getMapper(StudenDao.class); int i = mapper.updateStudent(new Student(0,"10005","李四","男",20)); if (i>0){ System.out.printf("修改成功"); } sqlSession.commit(); sqlSession.close(); } @Test//查询用户 public void TestListStudent(){ SqlSession sqlSession = Mybatis.getsqlSeesion(); StudenDao mapper = sqlSession.getMapper(StudenDao.class); List<Student> students = mapper.ListStudent(); for (Student student1 : students) { System.out.println(student1); } sqlSession.close(); } @Test//分页查询 public void TestListstudents(){ SqlSession sqlSession = Mybatis.getsqlSeesion(); StudenDao mapper = sqlSession.getMapper(StudenDao.class); PageHelper.startPage(1,3); List<Student> students = mapper.ListStudents(); PageInfo<Student> pageInfo =new PageInfo<Student>(students); List<Student> list = pageInfo.getList(); for (Student student1 : list) { System.out.println(student1); } }