文章目录 XML映射器操作(XxxMapper.xml) 文件目录 1.基本介绍 2.环境配置 1.在原来的父模块下创建一个子模块 2.删除没用的两个文件夹 3.创建基本目录 4.父模块的pom.xml 5.jdbc.properties 6.mybatis-config.xml 7.测试使用MonsterMapperTest.java 3.parameterType(输入参数类型) 1.基本介绍 2.案例一 1.MonsterMapper.java 2.MonsterMapper.xml 3.测试 3.案例二(模糊查询) 1.MonsterMapper.java添加方法 2.MonsterMapper.xml添加实现类 3.测试 4.传入HashMap 1.基本介绍![image-20240304135016653](https://img-blog.csdnimg.cn/img_convert/6ece40de84bcc9416cce823c98dd14c1.png) 2.案例一(参数为map) 1.MonsterMapper.java添加方法 2.MonsterMapper.xml 3.测试 3.案例二(返回值为map) 1.MonsterMapper.java添加方法 2.MonsterMapper.xml 3.测试 5.ResultMap 1.基本介绍 2.案例 1.创建数据表 2.创建映射类User.java 3.UserMapper.java 4.UserMapper.xml 5.测试(数据未填充!) 6.使用resultMap替换resultType解决,修改UserMapper.xml 7.结果 3.注意事项与细节
XML映射器操作(XxxMapper.xml)
文件目录
1.基本介绍
1.优点
2.常用顶级元素
2.环境配置
1.在原来的父模块下创建一个子模块
2.删除没用的两个文件夹
3.创建基本目录
4.父模块的pom.xml
< 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> < groupId> org.example</ groupId> < artifactId> mybatis</ artifactId> < version> 1.0-SNAPSHOT</ version> < packaging> pom</ packaging> < name> Archetype - mybatis</ name> < url> http://maven.apache.org</ url> < modules> < module> mybatis_quickstart</ module> < module> xml-mapper</ module> </ modules> < dependencies> < dependency> < groupId> mysql</ groupId> < artifactId> mysql-connector-java</ artifactId> < version> 5.1.49</ version> </ dependency> < dependency> < groupId> org.mybatis</ groupId> < artifactId> mybatis</ artifactId> < version> 3.5.7</ version> </ dependency> < dependency> < groupId> junit</ groupId> < artifactId> junit</ artifactId> < version> 4.12</ version> < scope> test</ scope> </ dependency> </ dependencies> < build> < resources> < resource> < directory> src/main/java</ directory> < includes> < include> **/*.xml</ include> </ includes> </ resource> < resource> < directory> src/main/resources</ directory> < includes> < include> **/*.xml</ include> < include> **/*.properties</ include> </ includes> </ resource> </ resources> </ build>
</ project>
5.jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
6.mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd" >
< configuration> < properties resource = " jdbc.properties" /> < settings> < setting name = " logImpl" value = " STDOUT_LOGGING" /> </ settings> < typeAliases> < package name = " com.sun.entity" /> </ typeAliases> < environments default = " development" > < environment id = " development" > < transactionManager type = " JDBC" /> < 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> < mappers> < package name = " com.sun.mapper" /> </ mappers> </ configuration>
7.测试使用MonsterMapperTest.java
package com. sun. mapper ; import com. util. MyBatisUtils ;
import org. apache. ibatis. session. SqlSession ;
import org. junit. Before ;
import org. junit. Test ;
public class MonsterMapperTest { private SqlSession sqlSession; private MonsterMapper monsterMapper; @Before public void init ( ) { sqlSession = MyBatisUtils . getSqlSession ( ) ; monsterMapper = sqlSession. getMapper ( MonsterMapper . class ) ; System . out. println ( monsterMapper. getClass ( ) ) ; } @Test public void test ( ) { System . out. println ( "ssss" ) ; }
}
3.parameterType(输入参数类型)
1.基本介绍
2.案例一
1.MonsterMapper.java
package com. sun. mapper ; import com. sun. entity. Monster ; import java. util. List ;
public interface MonsterMapper { public List < Monster > findMonsterByNameORId ( Monster monster) ; }
2.MonsterMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " com.sun.mapper.MonsterMapper" > < select id = " findMonsterByNameORId" parameterType = " Monster" resultType = " Monster" > SELECT * FROM monster WHERE id = #{id} OR `name` = #{name}</ select>
</ mapper>
3.测试
package com. sun. mapper ; import com. sun. entity. Monster ;
import com. util. MyBatisUtils ;
import org. apache. ibatis. session. SqlSession ;
import org. junit. Before ;
import org. junit. Test ; import java. util. List ;
public class MonsterMapperTest { private SqlSession sqlSession; private MonsterMapper monsterMapper; @Before public void init ( ) { sqlSession = MyBatisUtils . getSqlSession ( ) ; monsterMapper = sqlSession. getMapper ( MonsterMapper . class ) ; System . out. println ( monsterMapper. getClass ( ) ) ; } @Test public void test ( ) { Monster monster = new Monster ( ) ; monster. setId ( 1 ) ; monster. setName ( "大象精" ) ; List < Monster > monsterList = monsterMapper. findMonsterByNameORId ( monster) ; for ( Monster monster1 : monsterList) { System . out. println ( monster1) ; } if ( sqlSession != null ) { sqlSession. close ( ) ; } }
}
3.案例二(模糊查询)
1.MonsterMapper.java添加方法
public List < Monster > findMonsterByName ( String name) ;
2.MonsterMapper.xml添加实现类
< select id = " findMonsterByName" parameterType = " String" resultType = " Monster" > SELECT * FROM monster WHERE `name` LIKE '%${name}%'</ select>
3.测试
@Test public void MonsterMapper ( ) { List < Monster > monsterList = monsterMapper. findMonsterByName ( "孙" ) ; for ( Monster monster : monsterList) { System . out. println ( monster) ; } }
4.传入HashMap
1.基本介绍
2.案例一(参数为map)
1.MonsterMapper.java添加方法
public List < Monster > findMonsterByIdAndName_ParamMap ( Map < String , Object > map) ;
2.MonsterMapper.xml
< select id = " findMonsterByIdAndName_ParamMap" parameterType = " Map" resultType = " Monster" > SELECT * FROM monster WHERE id = #{id} OR `name` = #{name}</ select>
3.测试
@Test public void findMonsterByIdAndName_ParamMap ( ) { Map map = new HashMap < String , Object > ( ) ; map. put ( "id" , 1 ) ; map. put ( "name" , "大象精" ) ; List < Monster > monsters = monsterMapper. findMonsterByIdAndName_ParamMap ( map) ; for ( Monster monster : monsters) { System . out. println ( monster) ; } }
3.案例二(返回值为map)
1.MonsterMapper.java添加方法
public List < Map < String , Object > > findMonsterByIdAndName_ParamMap_returnMap ( Map < String , Object > map) ;
2.MonsterMapper.xml
< select id = " findMonsterByIdAndName_ParamMap_returnMap" parameterType = " Map" resultType = " Map" > SELECT * FROM monster WHERE id = #{id} OR `name` = #{name}</ select>
3.测试
@Test public void findMonsterByIdAndName_ParamMap_returnMap ( ) { Map map = new HashMap < String , Object > ( ) ; map. put ( "id" , 6 ) ; map. put ( "name" , "大象精" ) ; List < Map < String , Object > > mapList = monsterMapper. findMonsterByIdAndName_ParamMap_returnMap ( map) ; for ( Map < String , Object > stringObjectMap : mapList) { for ( Map. Entry < String , Object > entry : stringObjectMap. entrySet ( ) ) { System . out. println ( entry. getKey ( ) + " " + entry. getValue ( ) ) ; } } }
5.ResultMap
1.基本介绍
2.案例
1.创建数据表
CREATE TABLE ` user` ( user_id INT NOT NULL auto_increment , user_email VARCHAR ( 255 ) DEFAULT '' , user_name VARCHAR ( 255 ) DEFAULT '' , PRIMARY KEY ( user_id)
)
2.创建映射类User.java
package com. sun. entity ; public class User { private Integer user_id; private String username; private String useremail; public User ( ) { } public User ( Integer user_id, String username, String useremail) { this . user_id = user_id; this . username = username; this . useremail = useremail; } public Integer getUser_id ( ) { return user_id; } public void setUser_id ( Integer user_id) { this . user_id = user_id; } public String getUsername ( ) { return username; } public void setUsername ( String username) { this . username = username; } public String getUseremail ( ) { return useremail; } public void setUseremail ( String useremail) { this . useremail = useremail; } @Override public String toString ( ) { return "User{" + "user_id=" + user_id + ", username='" + username + '\'' + ", useremail='" + useremail + '\'' + '}' ; }
}
3.UserMapper.java
UserMapper . xmlpackage com. sun. mapper; import com. sun. entity. User ; import java. util. List ;
public interface UserMapper { public List < User > findAllUser ( ) ;
}
4.UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " com.sun.mapper.UserMapper" > < select id = " findAllUser" resultType = " User" > SELECT * FROM user</ select>
</ mapper>
5.测试(数据未填充!)
package com. sun. mapper ; import com. sun. entity. User ;
import com. util. MyBatisUtils ;
import org. apache. ibatis. session. SqlSession ;
import org. junit. Before ;
import org. junit. Test ; import java. util. List ;
public class UserMapperTest { private SqlSession sqlSession; private UserMapper userMapper; @Before public void init ( ) { sqlSession = MyBatisUtils . getSqlSession ( ) ; userMapper = sqlSession. getMapper ( UserMapper . class ) ; System . out. println ( userMapper. getClass ( ) ) ; } @Test public void findAllUser ( ) { List < User > allUser = userMapper. findAllUser ( ) ; System . out. println ( allUser) ; }
}
6.使用resultMap替换resultType解决,修改UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " com.sun.mapper.UserMapper" > < resultMap id = " findAllUserResultType" type = " User" > < result column = " user_email" property = " useremail" /> < result column = " user_name" property = " username" /> </ resultMap> < select id = " findAllUser" resultMap = " findAllUserResultType" > SELECT * FROM user</ select>
</ mapper>
7.结果
3.注意事项与细节
如果没有使用resultMap,但是表的列名与映射类的属性不匹配,则返回的结果不会填充,则该位置会是对象属性的默认值 解决列名与属性名不同也可以使用别名,简单来说就是查询出来结果的时候使用as ,将结果所在的列名设置成跟属性一致的名字,但是不推荐 MyBatis-Plus可以直接使用@TableField注解或者@TableName注解来解决