mybatis中#{}和${}的区别
程序员文章站
2022-06-10 08:51:04
...
#{}
#{}表示一个占位符号,通过#{}可以实现perparedStatement向占位符中设置值,自动进行java类型和jdbc类型转换
#{}可以接收简单类型值或pojo属性值
${}
${}表示拼接sql串,通过${}可以将parameterType传入的内容拼接在sql中且不进行jdbc类型转换
${}可以接收简单类型值或pojo属性值,如果parameterType传入单个简单类型值,${}括号中只能是value
SqlMapConfig.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>
<properties resource="jdbc.properties"></properties>
<environments default="mysql">
<environment id="mysql">
<!--事务管理器-->
<transactionManager type="jdbc"></transactionManager>
<!--连接池配置-->
<dataSource type="pooled">
<property name="driver" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</dataSource>
</environment>
</environments>
</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="com.wang.dao.IUserDao">
<insert id="save" parameterType="com.wang.entity.User">
INSERT INTO user (username,sex,birthday,address) VALUES (#{username},#{sex},#{birthday},#{address})
</insert>
</mapper>