注意事项:
1 mapper配置文件中路径用 . 比如namespace="com.daoimpl.StuDaoImpl"而不是namespace="com/daoimpl/StuDaoImpl"。
2 在dao接口要用代理对象连接mapper配置文件,须使用StuDaoImpl stuDao=session.getMapper(StuDaoImpl.class)方法,然后stuDao.方法名();调用后会在namespace中寻找,此时需注意是namespace="com/daoimpl/StuDaoImpl"而不是namespace="com/daoimpl/StuDaoImplMapper",也就是说namespace的包关系要能准确的对应接口名,然后才能与方法匹配。
3 使用注解的时候路径关系是 . <mapper class="com.daoimpl.StuDaoImplMapper"/>
@Select("select * from student where SNo=#{sno} and Spassword=#{spassword}")
public Student loginStu(Student stu);
核心配置文件
<?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 >
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="url" value="jdbc:mysql://localhost:3306/testmybatis"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--注意注解的配置,StuDaoImpl后没有Mapper -->
<mapper class="com.daoimpl.StuDaoImpl"/>
<!--以下是*Mapper.xml的配置-->
<!-- <mapper resource="com/daoimpl/StuDaoImplMapper.xml"/> -->
</mappers>
</configuration>
4 使用配置文件的路径关系是 / <mapper resource="com/daoimpl/StuDaoImplMapper.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中 *定义为要映射的POJO类名 -->
<mapper namespace="com.daoimpl.StuDaoImpl">
<select id="selectStu" parameterType="com.vo.Student" resultType="com.vo.Student">
select * from student
</select>
<!-- 接收Student stu 返回唯一对象student -->
<select id="loginStu" parameterType="com.vo.Student" resultType="com.vo.Student">
select * from student where SNo=#{sno} and Spassword=#{spassword}
</select>
</mapper>
5 总的方法实现
public class Domain {
public static SqlSession session;
public static void main(String[] args){
getCurrentSession();
Student stu=new Student();
stu.setSno(1);
stu.setSpassword("0");
//构建代理对象
StuDaoImpl stuDao=session.getMapper(StuDaoImpl.class);
//Student s=loginStu(stu);
Student s=stuDao.loginStu(stu);
System.out.println(s.getSname()+s.getSsex()+s.getScredit());
closeSession();
}
public static void getCurrentSession(){
try {
String resource="resources/mybatis-config.xml";
InputStream stream=Resources.getResourceAsStream(resource);
//根据配置文件构建sessionfactory
SqlSessionFactory sqlsession=new SqlSessionFactoryBuilder().build(stream);
//借助sqlSessionFactory创建sqlSession
session=sqlsession.openSession();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void closeSession(){
if(session!=null){
session.close();
}
}
}
6 导入dtd约束文件,解决xml文件不自动提示的问题:https://blog.csdn.net/jacksonary/article/details/79639990
7 在dao的接口层定义方法不能重名,不然根据配置文件创建工厂会失败。
8 在sql语句中有多个参数且来自不同表,占位符如何与之匹配的问题:
<1>.如果接口方法有一个或多个参数,并且使用了@Param注解,sql语句中的参数用注解的value值,
<2>.如果接口方法的参数只有一个,并且没有使用@Parma注解sql语句直接使用任何名称均可。
<3>.如果接口的方法有多个参数,并且没有使用@Parma注解,sql语句使用#{arg0}-#{argn}或者#{param1}-#{paramn}是不会错的。
例如:定义接口的方法的时候,sno来自学生表,cno来自课程表
//接口中方法的定义
public int delectSc(@Param("sno")int sno,@Param("cno")int cno);
//Mapper配置文件中方法的实现
<delete id="delectSc" parameterType="Integer" >
DELETE FROM sc WHERE sc.SNo=#{sno} AND sc.CNo=#{cno}
</delete>
<4>.sql语句中的参数占位符名称和接口方法的参数名称没有什么关系。