1...pom.xml 配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.wsc.mybatis</groupId> 8 <artifactId>testMybatis05</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 12 <properties> 13 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 14 <maven.compiler.source>1.8</maven.compiler.source> 15 <maven.compiler.target>1.8</maven.compiler.target> 16 </properties> 17 <dependencies> 18 <dependency> 19 <groupId>junit</groupId> 20 <artifactId>junit</artifactId> 21 <version>4.12</version> 22 </dependency> 23 <dependency> 24 <groupId>mysql</groupId> 25 <artifactId>mysql-connector-java</artifactId> 26 <version>5.1.6</version> 27 </dependency> 28 <dependency> 29 <groupId>org.mybatis</groupId> 30 <artifactId>mybatis</artifactId> 31 <version>3.4.5</version> 32 </dependency> 33 <dependency> 34 <groupId>log4j</groupId> 35 <artifactId>log4j</artifactId> 36 <version>1.2.17</version> 37 </dependency> 38 </dependencies> 39 </project>
2...jdbc.properties
1 jdbc.driver=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://localhost:3306/studentdb 3 jdbc.name=root 4 jdbc.password=wsc
3...SqlMapperConfig.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <!-- 引入外部文件--> 7 <properties resource="jdbc.properties"></properties> 8 <!-- 插件--> 9 <!-- <plugins>--> 10 <!-- <plugin interceptor=""></plugin>--> 11 <!-- </plugins>--> 12 13 <!-- 类型转换--> 14 <!-- <typeHandlers></typeHandlers>--> 15 16 <!--类别名映射 <typeAliases type="com.wsc.domain.User" alias=User"> 17 包的映射 引入包中所有的pojo 类型 配置别名 :简单类名 不区分大小写 18 --> 19 <typeAliases> 20 <package name="com.wsc.domain"/> 21 </typeAliases> 22 <environments default="development"> 23 24 25 <environment id="development"> 26 27 <transactionManager type="JDBC"/> 28 <dataSource type="POOLED"> 29 <property name="driver" value="${jdbc.driver}"/> 30 <property name="url" value="${jdbc.url}"/> 31 <property name="username" value="${jdbc.name}"/> 32 <property name="password" value="${jdbc.password}"/> 33 </dataSource> 34 </environment> 35 </environments> 36 <mappers> 37 <mapper resource="com/wsc/dao/StudentMapper.xml"/> 38 </mappers> 39 </configuration>
4...StudentMapper.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="StudentDao"> 6 <select id="find" resultType="Student"> 7 select * from student; 8 </select> 9 <!-- 通过id查询--> 10 <select id="selectById" resultType="Student" parameterType="java.lang.Integer"> 11 SELECT * FROM student where studentNo=#{studentNo}; 12 </select> 13 <!-- 删除通过id--> 14 <delete id="drop" parameterType="java.lang.Integer"> 15 Delete from student where studentNo=#{studentNo}; 16 </delete> 17 <!-- 添加--> 18 <insert id="add" parameterType="Student"> 19 insert into student values (null,#{studentName},#{age},#{boreDate},#{classNo}); 20 </insert> 21 <!-- 修改数据--> 22 <update id="change" parameterType="Student" > 23 update student set studentName=#{studentName},age=#{age},boreDate=#{boreDate},classNo=#{classNo} where studentNo=#{studentNo}; 24 </update> 25 <!-- 模糊查询--> 26 <select id="queryByName" resultType="Student" parameterType="java.lang.String"> 27 select * from student where studentName like "%"#{studentName}"%"; 28 </select> 29 </mapper>
5...dao层
1 package com.wsc.dao; 2 3 import com.wsc.domain.Student; 4 5 import java.util.List; 6 7 8 /** 9 * @version 1.0 10 * @ClassName StudentDao 11 * @Description TODO 12 * @Author WSC 13 * @Date 2019/7/1 14:32 14 **/ 15 public interface StudentDao { 16 /** 17 * 查询所有数据 18 * @return 数据集合 19 */ 20 public List<Student> find(); 21 /** 22 * 通过id删除 23 * @param id 24 */ 25 public void drop(Integer id); 26 27 /** 28 * 通过id 查询 29 * @param id 30 */ 31 public List<Student> selectById(Integer id); 32 33 /** 34 * 通过对象 添加数据 35 * @param student 36 */ 37 public void add(Student student); 38 39 /** 40 * 模糊查询 41 * @param name 42 */ 43 public List<Student> queryByName(String name); 44 45 /** 46 * 通过id 修改数据 47 * 48 */ 49 public void change(Student student); 50 }
6...test
1 package com.wsc.testMybatis; 2 3 import com.wsc.dao.StudentDao; 4 import com.wsc.domain.Student; 5 import org.apache.ibatis.session.SqlSession; 6 import org.apache.ibatis.session.SqlSessionFactory; 7 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 8 import org.junit.Before; 9 import org.junit.Test; 10 11 import java.util.Date; 12 import java.util.List; 13 14 /** 15 * @version 1.0 16 * @ClassName TestStudent 17 * @Description TODO 18 * @Author WSC 19 * @Date 2019/7/1 14:43 20 **/ 21 public class TestStudent { 22 public SqlSessionFactory sqlSessionFactory; 23 @Before 24 public void init(){ 25 sqlSessionFactory = new SqlSessionFactoryBuilder().build(this.getClass().getClassLoader().getResourceAsStream("SqlMapperConfig.xml")); 26 } 27 28 /** 29 * 查询所有 30 */ 31 @Test 32 public void selectAll(){ 33 SqlSession sqlSession = sqlSessionFactory.openSession(); 34 StudentDao mapper = sqlSession.getMapper(StudentDao.class); 35 List<Student> all = mapper.find(); 36 for(Student student:all){ 37 System.out.println(student); 38 } 39 sqlSession.close(); 40 } 41 42 /** 43 * 删除通过id 44 */ 45 @Test 46 public void dropStudent(){ 47 SqlSession sqlSession = sqlSessionFactory.openSession(); 48 StudentDao mapper = sqlSession.getMapper(StudentDao.class); 49 mapper.drop(15); 50 sqlSession.commit(); 51 sqlSession.close(); 52 } 53 @Test 54 public void addStudent(){ 55 SqlSession sqlSession = sqlSessionFactory.openSession(); 56 StudentDao mapper = sqlSession.getMapper(StudentDao.class); 57 Student student = new Student(); 58 student.setStudentName("sqwe国"); 59 student.setAge(20); 60 Date date = new Date(); 61 // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); 62 // String format = simpleDateFormat.format(date); 63 // try { 64 // Date parse = simpleDateFormat.parse(format); 65 // System.out.println(parse); 66 // student.setBoreDate(parse); 67 // } catch (ParseException e) { 68 // e.printStackTrace(); 69 // } 70 student.setBoreDate(date); 71 student.setClassNo(1); 72 mapper.add(student); 73 sqlSession.commit(); 74 sqlSession.close(); 75 } 76 }