<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.pg</groupId> <artifactId>maven-three</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>maven-three</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
<?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://192.168.159.128:3306/leocms?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="leo@0362"/> </dataSource> </environment> </environments> <mappers> <mapper resource="cn/pg/mybatis/catMapper.xml"></mapper> </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"> <mapper namespace="cn.pg.mybatis.CatMapper"> <select id="getCatList" resultType="cn.pg.mybatis.Cat"> SELECT * FROM cat; </select> <select id="getCatById" resultType="cn.pg.mybatis.Cat" parameterType="int"> SELECT * FROM cat WHERE id=#{id}; </select> <insert id="addCat" parameterType="cn.pg.mybatis.Cat" > insert into cat (`name`,`colour`) values (#{name},#{colour}); </insert> <update id="updateCat" parameterType="cn.pg.mybatis.Cat" > update cat set name=#{name},colour=#{colour} where id=#{id}; </update> <delete id="deleteCatById" parameterType="int"> delete from cat where id=#{id}; </delete> </mapper>
package cn.pg.mybatis; public class Cat { private int id; private String name; private String colour; public Cat() { super(); // TODO 自动生成的构造函数存根 } public Cat(int id, String name, String colour) { super(); this.id = id; this.name = name; this.colour = colour; } public Cat(String name, String colour) { super(); this.name = name; this.colour = colour; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColour() { return colour; } public void setColour(String colour) { this.colour = colour; } @Override public String toString() { return "Cat [id=" + id + ", name=" + name + ", colour=" + colour + "]"; } }
package cn.pg.mybatis; import java.util.List; public interface CatMapper { List<Cat> getCatList(); Cat getCatById(int id); int addCat(Cat cat); int updateCat(Cat cat); int deleteCatById(int id); }
package cn.pg.mybatis; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.junit.Test; public class MyTest { public static void main(String[] args) { System.out.println("afafaf"); SqlSession sqlSession=MybatisUtils.getSqlSession(); CatMapper catMapper=sqlSession.getMapper(CatMapper.class); List<Cat> catList=catMapper.getCatList(); for (Cat cat : catList) { System.out.println(cat); } sqlSession.close(); } @Test public void getCatById() { SqlSession sqlSession=MybatisUtils.getSqlSession(); CatMapper catMapper=sqlSession.getMapper(CatMapper.class); Cat cat=catMapper.getCatById(3); System.out.println(cat); sqlSession.close(); } @Test public void addCat() { SqlSession sqlSession=MybatisUtils.getSqlSession(); CatMapper catMapper=sqlSession.getMapper(CatMapper.class); Cat cat=new Cat("afaf","fafafaf"); catMapper.addCat(cat); sqlSession.commit(); sqlSession.close(); } @Test public void updateCat() { SqlSession sqlSession=MybatisUtils.getSqlSession(); CatMapper catMapper=sqlSession.getMapper(CatMapper.class); Cat cat=new Cat(4,"afaf","fafafaf"); catMapper.updateCat(cat); sqlSession.commit(); sqlSession.close(); } @Test public void deleteCatById() { SqlSession sqlSession=MybatisUtils.getSqlSession(); CatMapper catMapper=sqlSession.getMapper(CatMapper.class); catMapper.deleteCatById(3); sqlSession.commit(); sqlSession.close(); } }
原文:https://www.cnblogs.com/leo0362/p/13808757.html