1、junit方法 @Test @Before @After
2、数据类型与别名
3、参数定义方法:
#{name} 直接表示对象的属性名,只有一个参数的时候 #{} 内的名称随便写。
'%${name}%' 模糊查询拼接字符串
<select id="findByUsernameLike" parameterType="string" resultType="com.itbaizhan.pojo.User"> select * from user where username like '%${value}%' </select>
模糊查询的推荐写法,使用bind标签
<select id="findByUsernameLike" parameterType="string" resultType="com.itbaizhan.pojo.User"> <bind name="likeName" value="'%'+username+'%'"/> select * from user where username like # {likeName} </select>
4、多参书写方式,
注解方式
List<User> findPage1(@Param("startIndex") int startIndex, @Param("pageSize")int pageSize);
<select id="findPage1" resultType="com.itbaizhan.mapper.User"> select * from user limit #{startIndex},# {pageSize} </select>
Map传参
List<User> findPage3(Map<String,Object> params);
<select id="findPage3" resultType="com.itbaizhan.pojo.User" parameterType="map"> select * from user limit # {startIndex},#{pageSize} </select>
@Test public void testFindPage3(){ Map<String,Object> params = new HashMap(); params.put("startIndex",0); params.put("pageSize",4); List<User> users = userMapper.findPage3(params); users.forEach(System.out::println); }
5、主键回填
void add(User user);
<insert id="add" parameterType="com.itbaizhan.user.User"> <!-- keyProperty:主键属性名,keyColumn:主 键列名,resultType:主键类型,order:执行时机 --> <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> <!-- SELECT LAST_INSERT_ID():查询刚刚插入的记录的主键 值,只适用于自增主键,且必须和insert语句一起执行。-->
@Test public void testAdd(){ User user = new User("尚学堂", new Date(), "男", "北京"); userMapper.add(user); session.commit(); System.out.println(user.getId()); }
6、正确配置数据库连接的方法
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybat is jdbc.username=root jdbc.password=root在配置文件中引入db.properties
<properties resource="db.properties"> </properties> <environments default="mysql"> <environment id="mysql"> <transactionManager type="JDBC"> </transactionManager> <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>
7、类别名
<!-- mybatis配置文件 --> <typeAliases> <typeAlias type="com.itbaizhan.pojo.User" alias="User"> </typeAlias> </typeAliases>
映射文件
<select id="findAll" resultType="User"> select * from user </select>为一个所有包下的所有类配置别名
<typeAliases> <package name="com.itbaizhan.pojo"> </package> </typeAliases>
8、 <mappers> 用于注册映射文件或持久层接口
<!-- 注册一个包下的所有持久层接口 --> <mappers> <package name="com.itbaizhan.mapper"/> </mappers
9、返回属性与对象名不一致时
resultMap
<!-- id:自定义映射名 type:自定义映射的对象类型 --> <resultMap id="teacherMapper" type="com.itbaizhan.pojo.Teacher"> <!-- id定义主键列 property:POJO属性名 column:数据库列名 --> <id property="id" column="tid"></id> <!-- result定义普通列 property:POJO属性 名 column:数据库列名 --> <result property="teacherName" column="tname"></result> </resultMap> <select id="findAll" resultMap="teacherMapper"> select * from teacher </select>
sql语句as
<select id="findAll" resultType="com.itbaizhan.pojo.Teacher"> select tid as id,tname as teacherName from teacher; </select>
10、复用sql方法
<sql id="selectAllField"> select tid as id,tname as teacherName </sql> <select id="findAll" resultType="com.itbaizhan.pojo.Teacher"> <include refid="selectAllField"> </include> from teacher; </select> <select id="findById" resultType="com.itbaizhan.pojo.Teacher"> <include refid="selectAllField"></include> from teacher where tid = #{id} </select>
11、MyBatis映射文件_特殊字符处理
<select id="findById2" resultType="com.itbaizhan.pojo.Teacher"> <include refid="selectAllField"> </include> from teacher where tid > #{id} </select>
标签:jdbc,pageSize,startIndex,关于,mybatis,teacher,select,user From: https://www.cnblogs.com/Mvloveyouforever/p/18402896