这周认真学了下ssm框架,先讲mybatis吧
1.目录结构
由于mybatis是应用于Dao层,可以直接创建普通的java项目
2.jar包
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
3.核心配置文件conf.xml
<?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="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/demo"/>
<property name="username" value="root"/>
<property name="password" value="98629"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/UserMapper.xml"></mapper>
</mappers>
</configuration>
主要配置datasource和关联xxxMapper.xml文件,在idea中,conf.xml文件需要放在resource文件夹中,否则会报错找不到该文件。
4.xxxMapper.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 namespace="mapper.UserMapper">
<!-- 通过id查找User-->
<select id="selectaById" resultType="domain.User" parameterType="int">
select * from user where id = #{id}
</select>
<!-- 查找全部-->
<select id="selectAll" resultType="domain.User" >
select * from user
</select>
</mapper>
该文件主要写sql语句,用于查询,id为代理对象的方法名,resultType为代理对象的返回值,parameterType为输入值类型。
5.动态代理对象
public interface UserMapper {
User selectaById(int id);
}
通过动态代理,可以直接调用xxxMapper.xml的sql语句。
6.具体实现类
public class UserDao {
public static void main(String[] args) throws IOException {
// 读取conf.xml
Reader reader = Resources.getResourceAsReader("conf.xml");
// 创建会话工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = sessionFactory.openSession();
// 通过id查找
// String statement = "domain.User.selectaById";
// User user = sqlSession.selectOne(statement, 1);
// System.out.println(user);
// 查找全部
// String statement = "domain.User.selectAll";
// List<User> list = sqlSession.selectList(statement);
// System.out.println(list);
// 通过约定查找,通过id
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.selectaById(1);
System.out.println(user);
sqlSession.close();
}
}
第一步,读取文件流
Reader reader = Resources.getResourceAsReader("conf.xml");
第二步,创建SqlSessionFactory
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
第三步,开启
SqlSession sqlSession = sessionFactory.openSession();
第四步,通过getMapper,创建该接口对象
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
第五步,调用接口方法
User user = userMapper.selectaById(1);
第六步,关闭
sqlSession.close();
Mybatis也可以一对多,多对多的查找数据库,返回类型要单独将两个实体类连接起来,例如
多对多查找用户账号中的学生信息
实体类:
public class UserAndStudent {
private User user;
private Student student;
}
xxxMapper.xml
<select id="selectInfo" parameterType="int" resultMap="StudentAndUserSelect">
select student.*,user.*
from student,user
where student.id=user.id and user.id = #{id}
</select>
<resultMap id="StudentAndUserSelect" type="domain.Json.UserAndStudent">
<association property="user" javaType="domain.User">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="password" column="password"></result>
</association>
<association property="student" javaType="domain.Student">
<id property="id" column="id"></id>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
</association>
</resultMap>
这样即可完成多对多的查询。