一、目录结构
二、代码
1、pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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>org.example</groupId> <artifactId>A01mybatis</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!--引入mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.1</version> </dependency> <!--引入mysql驱动包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> <!--引入log4j--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> </dependencies> <build> <plugins> <!--指定maven编译器版本--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
2、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> <!--指定别名后mapper文件resultType可以省略包名--> <typeAliases> <package name="com.wuxi.beans"/> </typeAliases> <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.2.105:3306/ssm?characterEncoding=utf8&useSSL=false"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <!--指定mapper文件位置--> <mappers> <package name="com.wuxi.daos"/> </mappers> </configuration>
3、log4j.properties
log4j.logger.com.wuxi.daos=debug,console log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=[%d{HH:mm:ss}] %m%n
4、Student.java
package com.wuxi.beans; public class Student { private Integer id; private String name; private Integer age; private Integer sex; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name == null ? null : name.trim(); } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } @Override public String toString() { return "Student:{id:" + id + ", name:" + name + ", age:" + age + ", sex:" + sex + "}"; } }
5、StudentMapper.java
package com.wuxi.daos; import com.wuxi.beans.Student; import java.util.List; import java.util.Map; public interface StudentMapper { Integer insertStudent(Student student); List<Student> selectAllStudent(Map map); }
6、StudentMapper.xml(必须放在resources目录下。要放在java目录下需做下方pom.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="com.wuxi.daos.StudentMapper"> <insert id="insertStudent"> insert into student(name, age, sex) values(#{name}, #{age}, #{sex}) <selectKey resultType="int" keyProperty="id" order="AFTER"> select @@identity </selectKey> </insert> <select id="selectAllStudent" resultType="Student"> select id, name, age, sex from student where name like #{student.name} </select> </mapper>
<build> <resources> <!-- src/main/java目录下的mapper.xml文件,需此配置才能打进包里 --> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> </build>
7、MybatisTest.java
package com.wuxi; import com.wuxi.beans.Student; import com.wuxi.daos.StudentMapper; 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 java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; public class MybatisTest { public static void main(String[] args) { SqlSession session = null; try { String resource = "mybatis.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); session = sqlSessionFactory.openSession(); StudentMapper mapper = session.getMapper(StudentMapper.class); /* // 增 Student student = new Student(); student.setName("林允儿"); student.setAge(16); student.setSex(0); mapper.insertStudent(student); // student的id为插入时的id System.out.println(student); */ // 模糊查询 Map<String, Object> map = new HashMap<String, Object>(); Student student = new Student(); // %占位符不要写在mapper文件的sql中 student.setName("%" + "美" + "%"); map.put("student", student); // 使用map传参和ognl表达式 List<Student> students = mapper.selectAllStudent(map); System.out.println(students); session.commit(); } catch (Exception e) { e.printStackTrace(); } finally { if (session != null) { session.close(); } } } }