在Mybatis配置映射文件中,select
,insert
,update
,delete
等元素支持parameterType
属性。
parameterType
只要功能用来指定输入参数的类型,类型可以是简单类型、HashMap类型、pojo的包装类型。
首先创建一个Mybatis工程 【Mybatis】Mybatis开发入门程序
在数据库中创建一个名字为user
的,给该表增加3个字段分别是id、name、age
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) AUTO_INCREMENT=38 DEFAULT CHARSET=utf8;
UserMapper
public interface UserMapper {
User findUserById(int id);
}
配置文件中添加findUserById
方法
<select id="findUserById" parameterType="java.lang.Integer"
resultType="com.lucas.mybatis.model.User">
SELECT * FROM USER WHERE id = #{
id}
</select>
添加测试代码
InputStream inputstream;
try {
//获取Mybatis容器内容
inputstream = Resources.getResourceAsStream("mybatis-config.xml");
if (sqlsessionfactory == null) {
//新建一个factory,作用用来生产sqlSeesion
sqlsessionfactory = new SqlSessionFactoryBuilder().build(inputstream);
}
//使用factory的open方法创建一个sqlSeesion
SqlSession sqlsession = sqlsessionfactory.openSession();
UserMapper userMapper =sqlsession.getMapper(UserMapper.class);
// 根据Id查询数据
User userOne = userMapper.findUserById(2);
System.out.println(userOne);
sqlsession.commit(); //提交事务
sqlsession.close();//关闭连接
} catch (IOException e) {
e.printStackTrace();
}
UserMapper
public interface UserMapper {
List<User> findUsersByPojo(User user);
}
配置文件中添加findUsersByPojo
方法
<!-- parameterType传递pojo类型 -->
<select id="findUsersByPojo" parameterType="com.lucas.mybatis.model.User"
resultType="com.lucas.mybatis.model.User">
SELECT * FROM USER WHERE name LIKE "%${name}%" or age = #{
age}
</select>
添加测试代码
InputStream inputstream;
try {
//获取Mybatis容器内容
inputstream = Resources.getResourceAsStream("mybatis-config.xml");
if (sqlsessionfactory == null) {
//新建一个factory,作用用来生产sqlSeesion
sqlsessionfactory = new SqlSessionFactoryBuilder().build(inputstream);
}
//使用factory的open方法创建一个sqlSeesion
SqlSession sqlsession = sqlsessionfactory.openSession();
UserMapper userMapper = sqlsession.getMapper(UserMapper.class);
User user = new User();
user.setName("张");
user.setAge(18);
List<User> users = userMapper.findUsersByPojo(user);
for (User user1:users){
System.out.println(user1);
}
sqlsession.commit(); //提交事务
sqlsession.close();//关闭连接
} catch (IOException e) {
e.printStackTrace();
}
运行结果:
public interface UserMapper {
List<User> findUsersByMap(Map<String,Object> map);
}
配置文件中添加findUsersByMap
方法
<!-- parameterType传递hashmap类型 -->
<select id="findUsersByMap" parameterType="java.util.Map"
resultType="com.lucas.mybatis.model.User">
SELECT * FROM USER WHERE name LIKE "%${name}%" or age = #{
age}
</select>
添加测试代码
InputStream inputstream;
try {
//获取Mybatis容器内容
inputstream = Resources.getResourceAsStream("mybatis-config.xml");
if (sqlsessionfactory == null) {
//新建一个factory,作用用来生产sqlSeesion
sqlsessionfactory = new SqlSessionFactoryBuilder().build(inputstream);
}
//使用factory的open方法创建一个sqlSeesion
SqlSession sqlsession = sqlsessionfactory.openSession();
UserMapper userMapper = sqlsession.getMapper(UserMapper.class);
Map<String,Object> map = new HashMap<>();
map.put("name","张");
map.put("age",18);
List<User> users = userMapper.findUsersByMap(map);
for (User user1:users){
System.out.println(user1);
}
sqlsession.commit(); //提交事务
sqlsession.close();//关闭连接
} catch (IOException e) {
e.printStackTrace();
}