Mybatis crud操作
insert
首先在接口中声明一个对应的函数
void insertUser(User user);
然后再在mapper.xml配置文件中书写内容
<insert id="insertUser" parameterType="com.lucas.pojo.User">
insert into users(user, password, userType) value (#{user},#{password},#{userType})
</insert>
注意这里一定要写parameterType
属性,这是告知mybatis的参数类型的属性。
通过#{成员变量名}
来进行取得变量
还要注意在最后写一句话
sqlSession.commit();
提交事务,否则事务不会自动提交
update
void updateUser(User user);
<update id="updateUser" parameterType="com.lucas.pojo.User">
update users set user = #{user},password = #{password},userType = #{userType} where user = #{user};
</update>
delete
void deleteUser(String username);
<delete id="deleteUser" parameterType="String">
delete from users where user = #{user};
</delete>
如果参数类型是String类型,那么#{user}
这里面填什么都能被识别
模糊查询
List<User> findByName(String username);
<select id="findByName" parameterType="String" resultType="com.lucas.pojo.User">
select * from users where user like #{name};
</select>
注意这里的xml文件中没有写百分号,我们需要在调用的时候写上百分号
UserDao mapper = sqlSession.getMapper(UserDao.class);
List<User> list = mapper.findByName("2%");
使用聚合函数
int findUserNum();
<select id="findUserNum" resultType="int">
select count(*) from users;
</select>
插入并拿到id
<insert id="saveUser" parameterType="cn.ykf.pojo.User">
<selectKey keyProperty="id" keyColumn="id" resultType="int" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO user(username,birthday,sex,address) VALUES (#{username},#{birthday},#{sex},#{address})
</insert>
keyProperty
表示 selectKey 语句结果应该被设置的目标属性(对应实体类)。keyColumn
表示匹配属性的返回结果集中的列名称(对应数据库结果集)。order
可以被设置为 BEFORE 或 AFTER。如果设置为 BEFORE,那么它会首先生成主键,设置 keyProperty 然后执行插入语句。如果设置为 AFTER,那么先执行插入语句,然后再执行 selectKey 中的语句。
配置列名和属性名的对应关系
<!--type里面是对应的实体类-->
<resultMap id="userMap" type="com.lucas.pojo.User">
<!--主键对应-->
<id property="user" column="user"/>
<!--属性对应-->
<result property="password" column="password"/>
<result property="userType" column="userType"/>
</resultMap>
<select id="findAll" resultMap="userMap">
select * from users
</select>
标签的应用
基本使用
<properties>
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/education?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</properties>
通过再mybatis的主配置文件中加入这个标签,我们在下面可以通过表达式获取值。
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
resource属性
properties标签不仅可以通过基本的property标签配置,还可以导入外界的配置文件
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/education?serverTimezone=UTC
username=root
password=root
然后再xml中进行配置
<properties resource="jdbcProperties.properties">
</properties>
url属性
url是统一资源定位符
在这里,文件是遵循file协议的
<properties url="file:///D:/javaSPACE/myBatisLearn/src/main/resources/jdbcProperties.properties">
</properties>
配置别名
通过typeAliases
配置别名
<typeAliases>
<typeAlias type="com.lucas.pojo.User" alias="user"/>
</typeAliases>
type是全限定类名,alias是指定别名。只要指定别名就不限定大小写
<select id="findByName" parameterType="String" resultType="user">
select * from users where user like #{name};
</select>
标签
typeAliases的package
<typeAliases>
<package name="com.lucas.pojo"/>
</typeAliases>
用于指定要配置别名的包,当指定之后,该包下的实体都会注册别名,别名就是类名
mappers的package
这个package是用于指定dao接口所在的包,指定完之后就不用写class和resource了
<package name="com.lucas.dao"/>