
这里要有xml文件 不然测试会报错! 一定要注意这里编译文件
各个文件例子
1.mybatis.xml
version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//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"/><!--配置数据源:创建connection 对象--><dataSource type="POOLED"><!-- 驱动内容--><property name="driver" value="com.mysql.jdbc.Driver"/><!-- 链接数据的url--><property name="url" value="jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8"/><property name="username" value="root"/><property name="password" value="1234"/></dataSource></environment></environments><!--制定其他mapper文件的位置 (找到其他SQL语句)--><mappers><!-- 指定mapper文件的路径resourse="mapper 文件的路径 用/分来分割一个mapper resource 制定一个mapper文件--><mapper resource="com/bjpowernode/dao/StudentDao.xml"/></mappers></configuration>
2.studentDao.xml(在dao包)
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.bjpowernode.dao.StudentDao"><select id="selectStudentById"resultType="com.bjpowernode.entity.Student">select id,name,email,age from student</select></mapper><!--1.约束文件:"http://mybatis.org/dtd/mybatis-3-mapper.dtd"作用:定义和限制当前文件中可以使用的标签和属性,以及标签出现的顺序2.mapper是根标签(一个表一个mapper)namespace:命名空间必须有值 不能为空(唯一),推荐使用dao接口的全限定名称作用:识别SQL语句3.在mapper 里面可以写增删改查等标签(增删改查语句)4.select 标签 里面是select语句,id 推荐使用接口中得到方法名resultType(只在select标签 中使用)0 告诉mybatis 执行SQL语句把数据赋值给那个类型的Java对象推荐使用Java对象的全类名,如student类-->
3.实体类student(提供gettier和setter 还有toString 方法)
private Integer id;private String name;private String email;private Integer age;
4.dao接口
public interface StudentDao {//查询一个学生Student selectStudentById(Integer id);}
5.StudentTest 类(在下面的java包中)
public class MyTest {//测试mybatis 执行SQL语句@Testpublic void testSelectStudentById() throws IOException {//调用mybatis某个对象的方法,执行mapper文件的sql语句//定义mybatis住配置文件的位置,从类路径开始的相对路径String config = "mybatis.xml";//使用mybatis的resources类 读取配置文件InputStream inputStream = Resources.getResourceAsStream(config);//使用SqlSessionFactoryBuilder类,创建factory对象SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);//获取SqlSession对象SqlSession session = factory.openSession();//指定要执行的sql语句的id//sql语句的id =namespace+"."+增删改查的id属性值String sqlId = "com.bjpowernode.dao.StudentDao"+"."+"selectStudentById";//通过SqlSession方法执行sql语句Student student = session.selectOne(sqlId);System.out.println("使用mybatis查询一个学生:"+student);//关闭sqlsession对象session.close();}}
Expected one result (or null) to be returned by selectOne(), but found: 2

1.l另外一定要注意student.xml里面 语句有没有发生变化
设置日志(可以查看sql语句及变化)

<settings><setting name="logImpl" value="STDOUT_LOGGING"/></settings>
插入insert 但是数据库中表单信息没有变化
//mybatis 默认执行sql语句 手工提交,在执行update,insert delete 要提交事务
session.commit();
namespace+id(可以不用dao接口)
自定义(StudentDao)mapper.xml文件模板

mapper文件模板:<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="dao的全类名"><!--增删改查标签,sql语句--></mapper>
自定义mybatis.xml 文件的模板
mybatis文件模板:<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!-- 设置日志--><settings><setting name="logImpl" value="STDOUT_LOGGING"/></settings><environments default="mysql"><environment id="mysql"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8"/><property name="username" value="root"/><property name="password" value="1234"/></dataSource></environment></environments><mappers><!-- 指定mapper文件的路径resourse="mapper 文件的路径 用/分来分割一个mapper resource 制定一个mapper文件--><mapper resource="com/bjpowernode/dao/StudentDao.xml"/></mappers></configuration>
创建mybatis 工具类
public class MyBatisUtils {private static SqlSessionFactory factory=null;static {String config = "mybatis.xml";try {InputStream inputStream = Resources.getResourceAsStream(config);factory = new SqlSessionFactoryBuilder().build(inputStream);} catch (IOException e) {e.printStackTrace();}}//创建方法获取SqlSession 对象public static SqlSession getSqlSession(){SqlSession session = null;if (factory!=null){session = factory.openSession();}return session;}}
#{}和${}占位符
dao接口
//${}占位符
List<Student> select(@Param("name")String name );
//${}占位符
List<Student> select(@Param("name")String name,
@Param("colName")String colName);
studentdao.xml
<select id="select" resultType="com.bjpowernod.entity.Student">
select id,name,email,age from student where name=${name}
</select>
<select id="select" resultType="com.bjpowernod.entity.Student">
select id,name,email,age from student where name=#{name} order by ${colName} desc
</select>
test测试类(存在SQL注入)
@Test
public void selectByNameOrAge() {
SqlSession session = MyBatisUtils.getSqlSession();
StudentDao dao = session.getMapper(StudentDao.class);
List<Student> students = dao.select("'张三'or id>0");
students.forEach(student -> System.out.println("student=" + student));
session.close();
}
@Test
public void selectByNameOrAge() {
SqlSession session = MyBatisUtils.getSqlSession();
StudentDao dao = session.getMapper(StudentDao.class);
List<Student> students = dao.select("张三","id");
students.forEach(student -> System.out.println("student=" + student));
session.close();
}
resultType
resultType 自定义别名(推荐使用全限定名称)
1.第一种
- 在settings 后面 (mybatis.xml文件中)
- type :java类型的全限定名称(例如student 的)
- alias :自定义名称
```xml
mybatis.xml
studentDao.xml
优点:可以自定义<br />缺点:每个类都要单独自定义(类多可能比较繁琐)
2.第二种方式<br />同样在setting之后<br />name:包名,mybatis 会把这个包中的类作为别名xml
优点:一次性可以给多个类定义别名<br />缺点:别名不能自定义只能是类名(一个包中有同名 的类可能发生冲突)
<a name="vRkM3"></a>
## resultType返回对象 (同名列同名属性)
1.resultType 完全可以自定义<br /><br />
<a name="xAQS1"></a>
## resultType 返回简单类型
1.查询总数(查询平均数也以此类推)<br />2.studentDao.xmlxml
3.myTestjava
@Test
public void countStudent() {
//获取SqlSession 对象
SqlSession session = MyBatisUtils.getSqlSession();
StudentDao dao = session.getMapper(StudentDao.class);
long result = dao.countStudent();
System.out.println(“学生信息=” + result);
session.close();
}
4.studentDaojava
long countStudent();
<a name="dt6pt"></a>
## resultType 查询返回map
1.dao接口java
// 用map作为查询结果返回类型
Map
