1.2 写java package pojo ,dao
<?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>
<!--配置mybatis的环境-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--告知mybatis映射文件的位置-->
<mappers>
<mapper class="com.javaboy.dao.IUserDao"/>
</mappers>
</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.mybatis.dao.UserMapper">
<select id="findAllUser" resultType="com.mybatis.pojo.User">
select * from smbms_user;
</select>
<select id="findUserById" parameterType="Integer" resultType="com.mybatis.pojo.User">
select id,userCode,userName from smbms_user where id = #{uid}
</select>
<insert id="addUser" parameterType="User" >
insert into smbms_user (id,userCode,userName)
values (#{id},#{userCode},#{userName})
</insert>
</mapper>
注意事项:
-
-
dao层的接口名和映射文件的名字必须一致,且映射文件只能写在resources目录下
-
mapper映射文件中的namespace只能和接口的类路径保持一致
-
在mybatis-config.xml中元素标签的位置是有顺序的,顺序不对不能成功运行
-
记得将mapper映射文件引入到mybatis.xml文件中去
-
做sqlSession增删改的时候记得sqlSession.commit();
-
别名不区分大小写,增删改映射文件中是没有返回值类型的;int类型会默认返回.
-