学习的最大理由是想摆脱平庸,早一天就多一份人生的精彩;迟一天就多一天平庸的困扰。各位小伙伴,如果您:
想系统/深入学习某技术知识点…
一个人摸索学习很难坚持,想组团高效学习…
想写博客但无从下手,急需写作干货注入能量…
热爱写作,愿意让自己成为更好的人…
一、核心配置文件详解
二、默认的类型别名
三、MyBatis的增删改查
四、MyBatis获取参数值的两种方式
1、单个字面量类型的参数
2、多个字面量类型的参数
3、map集合类型的参数
4、实体类类型的参数
5、使用@Param标识参数
核心配置文件中的标签必须按照固定的顺序(有的标签可以不写,但顺序一定不能乱):
properties、settings、typeAliases、typeHandlers、objectFactory、objectWrapperFactory、reflectorFactory、plugins、environments、databaseIdProvider、mappers
DOCTYPE configuration
PUBLIC "-//MyBatis.org//DTD Config 3.0//EN"
"http://MyBatis.org/dtd/MyBatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties">properties>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
settings>
<typeAliases>
<package name="com.atguigu.mybatis.bean"/>
typeAliases>
<environments default="mysql_test">
<environment id="mysql_test">
<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.atguigu.mybatis.mapper"/>
mappers>
configuration>
<insert id="insertUser">
insert into t_user values(null,'admin','123456',23,'男','[email protected]')
insert>
<delete id="deleteUser">
delete from t_user where id = 6
delete>
<update id="updateUser">
update t_user set username = '张三' where id = 5
update>
<select id="getUserById" resultType="com.atguigu.mybatis.bean.User">
select * from t_user where id = 2
select>
<select id="getUserList" resultType="com.atguigu.mybatis.bean.User">
select * from t_user
select>
<select id="getUserByUsername" resultType="User">
select * from t_user where username = #{username}
select>
<select id="getUserByUsername" resultType="User">
select * from t_user where username = '${username}'
select>
<select id="checkLogin" resultType="User">
select * from t_user where username = #{arg0} and password = #{arg1}
select>
<select id="checkLogin" resultType="User">
select * from t_user where username = '${param1}' and password = '${param2}'
select>
<select id="checkLoginByMap" resultType="User">
select * from t_user where username = #{username} and password = #{password}
select>
@Test
public void checkLoginByMap() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
Map<String,Object> map = new HashMap<>();
map.put("usermane","admin");
map.put("password","123456");
User user = mapper.checkLoginByMap(map);
System.out.println(user);
}
<insert id="insertUser">
insert into t_user values(null,#{username},#{password},#{age},#{sex},#{email})
insert>
@Test
public void insertUser() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
User user = new User(null,"Tom","123456",12,"男","[email protected]");
mapper.insertUser(user);
}
<select id="CheckLoginByParam" resultType="User">
select * from t_user where username = #{username} and password = #{password}
select>
@Test
public void checkLoginByParam() {
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
mapper.CheckLoginByParam("admin","123456");
}
以上就是Mybatis之核心配置文件详解、默认类型别名、Mybatis获取参数值的两种方式的相关知识点,希望对你有所帮助。
积跬步以至千里,积怠惰以至深渊。时代在这跟着你一起努力哦!