import cn.kgc.entity.Person;
import cn.kgc.mapper.PersonMapper;
import cn.kgc.util.MytabisUtil;
import jdk.internal.util.xml.impl.Input;
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 TestMybatis {
@Test
public void testHelloWorld() throws IOException {
//1.通过流的机制获取主配置文件mybatis-config.xml的主要配置信息
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
//2.实例化SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//3.调用builder对象的builder()方法,获取SqlSessionFactory对象
SqlSessionFactory factory = builder.build(in);
//4.调用factory对象的openSession()方法,获取SqlSession对象
SqlSession sqlSession = factory.openSession();
//5.调用接口的方法
Integer count = sqlSession.getMapper(PersonMapper.class).findCount();
//6.输出对应的返回值count
System.out.println("count:" + count);
//7.关闭 SqlSession
sqlSession.close();
@Test
public void testInsert() throws Exception {
//1.通过流的机制获取主配置文件mybatis-config.xml的主要配置信息
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
//2.实例化SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//3.调用builder对象的builder()方法,获取SqlSessionFactory对象
SqlSessionFactory factory = builder.build(in);
//4.调用factory对象的openSession()方法,获取SqlSession对象
SqlSession sqlSession = factory.openSession();
//5.调用接口的方法
Person p = new Person();
p.setName("李四");
p.setNickname("一个人的夜晚上");
p.setAge(22);
sqlSession.getMapper(PersonMapper.class).addPerson(p);
//6.提交实物,该行代码必须放在关闭sqlsession之前
sqlSession.commit();
sqlSession.close();
@Test
public void testupdate() throws Exception {
//1.通过流的机制获取主配置文件mybatis-config.xml的主要配置信息
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
//2.实例化SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//3.调用builder对象的builder()方法,获取SqlSessionFactory对象
SqlSessionFactory factory = builder.build(in);
//4.调用factory对象的openSession()方法,获取SqlSession对象
SqlSession sqlSession = factory.openSession();
Person p2 = new Person();
p2.setId(2);
p2.setName("赵四");
p2.setNickname("来啊,造作啊");
p2.setAge(23);
sqlSession.getMapper(PersonMapper.class).updatePerson(p2);
//6.提交实物,该行代码必须放在关闭sqlsession之前
sqlSession.commit();
sqlSession.close();
@Test
public void testdel() throws Exception{
//1.通过流的机制获取主配置文件mybatis-config.xml的主要配置信息
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
//2.实例化SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//3.调用builder对象的builder()方法,获取SqlSessionFactory对象
SqlSessionFactory factory = builder.build(in);
//4.调用factory对象的openSession()方法,获取SqlSession对象
SqlSession sqlSession = factory.openSession();
sqlSession.getMapper(PersonMapper.class).delById(2);
sqlSession.commit();
sqlSession.close();
@Test
public void testdel2 () throws Exception{
//1.通过流的机制获取主配置文件mybatis-config.xml的主要配置信息
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
//2.实例化SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//3.调用builder对象的builder()方法,获取SqlSessionFactory对象
SqlSessionFactory factory = builder.build(in);
//4.调用factory对象的openSession()方法,获取SqlSession对象
SqlSession sqlSession = factory.openSession();
Person person = new Person();
person.setId(2);
sqlSession.getMapper(PersonMapper.class).delById2(person);
sqlSession.commit();
sqlSession.close();
@Test
public void testfindById () throws Exception{
//1.通过流的机制获取主配置文件mybatis-config.xml的主要配置信息
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
//2.实例化SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//3.调用builder对象的builder()方法,获取SqlSessionFactory对象
SqlSessionFactory factory = builder.build(in);
//4.调用factory对象的openSession()方法,获取SqlSession对象
SqlSession sqlSession = factory.openSession();
Person person = sqlSession.getMapper(PersonMapper.class).findById(1);
System.out.println("id"+ person.getId()+"name"+ person.getName()+"nickname"+ person.getNickname()+"age"+ person.getAge());
sqlSession.close();
@Test
public void testfindAll () throws IOException{
String str ="mybatis-config.xml";
InputStream in =Resources.getResourceAsStream(str);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
SqlSession sqlSession = factory.openSession();
List<Person> all = sqlSession.getMapper(PersonMapper.class).findAll();
for(Person p:all){
System.out.println(p);
sqlSession.close();
@Test
public void testfindByName () throws IOException{
String str ="mybatis-config.xml";
InputStream in =Resources.getResourceAsStream(str);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
SqlSession sqlSession = factory.openSession();
List<Person> list = sqlSession.getMapper(PersonMapper.class).findByName("wqz");
for(Person p:list){
System.out.println(p);
sqlSession.close();
@Test
public void testfindByPerson () throws IOException{
String str ="mybatis-config.xml";
InputStream in =Resources.getResourceAsStream(str);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
SqlSession sqlSession = factory.openSession();
Person person = new Person();
List<Person> list = sqlSession.getMapper(PersonMapper.class).findByPerson(person);
for(Person p:list){
System.out.println(p);
sqlSession.close();
@Test
public void findByPerson2 () throws Exception {
//1.通过流的机制获取主配置文件mybatis-config.xml的主要配置信息
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
//2.实例化SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//3.调用builder对象的builder()方法,获取SqlSessionFactory对象
SqlSessionFactory factory = builder.build(in);
//4.调用factory对象的openSession()方法,获取SqlSession对象
SqlSession sqlSession = factory.openSession();
String name = "王五";
String nickname = "123";
List<Person> list = sqlSession.getMapper(PersonMapper.class).findByPerson2(name,nickname);
for(Person p:list){
System.out.println("name:"+p.getName()+"nickname"+p.getNickname());
sqlSession.close();
@Test
public void testfindCount(){
SqlSession sqlSession = MytabisUtil.getSqlSession();
Integer count = sqlSession.getMapper(PersonMapper.class).findCount();
System.out.println(count);
@Test
public void findColList(){
SqlSession sqlSession = MytabisUtil.getSqlSession();
List<Person> colList = sqlSession.getMapper(PersonMapper.class).findColList("Id");
for (Person p:colList){
System.out.println(p);
}
pom.xml
代码语言:
javascript
代码
运行次数:
0
运行
复制
<?xml version="1.0" encoding="UTF-8"?>
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
<!-- $Id: pom.xml 642118 2008-03-28 08:04:16Z reinhard $ -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<name>20211125</name>
<groupId>org.example</groupId>
<artifactId>20211125</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.7</version>
<configuration>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>8888</port>
<maxIdleTime>30000</maxIdleTime>
</connector>
</connectors>
<webAppSourceDirectory>${
project.build.directory}/${
pom.artifactId}-${
pom.version}</webAppSourceDirectory>
<contextPath>/</contextPath>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--dependency>
<groupId>org.example</groupId>