JAVA面试要点003_Mybatis中#和$的区别
发布日期:2021-06-29 17:57:54 浏览次数:2 分类:技术文章

本文共 2378 字,大约阅读时间需要 7 分钟。

#的作用:
userMapper.xml
注意这里,写sql语句,咱们可以使用xml配置的方式,也可以使用注解的方式.
E:\workspace\day76_mybatis01\src\com\credream\test2\userMapper.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.credream.test2.userMapper">
<!-- 
CRUD操作
-->
//1.先添加一个增加操作
//parameterType="User"这个是传入一个对象
//
<insert id="addUser" parameterType="com.credream.bean.User">
insert into users(name, age) values(#{name}, #{age})
</insert>
//2.这里是删除操作
//id=#{id}注意这里的id=#{id}这个{id}中的id可以随意写的
//但是values(#{name}, #{age})不能随便写,因为
//(#{name}, #{age})的内容是从user的属性中获取的
//
 <delete id="deleteUser" parameterType="int">
delete from users where id=#{id}
</delete>
//3.更新操作
//#{name},age=#{age} where id=#{id}
//这三个都是从user对象中获取的.
//
<update id="updateUser" parameterType="com.credream.bean.User">
update users set name=#{name},age=#{age} where id=#{id}
</update>
<select id="getUser" parameterType="int" resultType="com.credream.bean.User">
select * from users where id=#{id}
</select>
//4.查询所有的:
//这个查询返回一个list,那这里返回一个List可以嘛?
//resultType="List",如果结果是一个List,他里面的内容是什么类型呢?
//User对吧.好,这个resultType="List"我先不写了,先不指定结果集类型
//一会再写
//
<select id="getAllUsers" >
select * from users
</select>

</mapper>

E:\workspace\day76_mybatis01\src\conf.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>
//1.这里这个地方咱们可以这样引入
//首先引入这个db.properties文件直接就可以
//<properties resource="db.properties"/>这样
//
<properties resource="db.properties">
//2.然后<property name="name" value="tt"/>是什么意思呢?
//这个意思是他可以覆盖db.properties文件中的内容
//
<property name="name" value="tt"/>
</properties>
<!-- 
development : 开发模式
work : 工作模式
 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
//3.引入了配置文件以后,这里在使用这些参数的时候就可以这样
//用类似与el表达式的写法了.
//
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${name}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/credream/test1/userMapper.xml"/>
<mapper resource="com/credream/test2/userMapper.xml"/>
<mapper class="com.credream.test3.UserMapper"/>
</mappers>
</configuration>

转载地址:https://credream.blog.csdn.net/article/details/72669442 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:JAVA面试要点004_JAVA编程过程中为了性能优化_应该注意到的地方
下一篇:JAVA面试要点002_Git中fetch和pull的区别

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月25日 15时10分07秒