注意与之前JDBC的区别,简化在哪里
-
准备工作
数据库,表的创建:
创建一个名为mybatis的数据库,在其中创建user表,字段为is,name,pwd
项目的准备:
新建一个普通的maven项目作为父项目,删除src目录,在pom文件中导入必要的依赖(mysql-connector-java,mybatis,junit),创建第一个子项目,选择父项目;
-
第一个mybatis程序
- 在resource目录中创建mybatis-config.xml配置文件,配置数据库驱动,url,name,password等信息,以及!扫描的mapper文件
<?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"> <!--可以配置多个不同的环境,都是固定配置,看mybatis开发手册--> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <!--配置url时要注意,可能需要时区的配置,且&在xml文件中需要转义--> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&useSSl=true"/> <property name="username" value="root"/> <property name="password" value=123456"/> </dataSource> </environment> </environments> <!--这个是一定一定要注意的,你得配置才能找到对应的sql文件--> <mappers> <mapper resource="com/test/dao/DaoMapper.xml"/> </mappers> </configuration>
*创建mybatis工具类,用它来连接数据库,并获取到执行sql语句的对象
import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class MybatisUtils { private static SqlSessionFactory sqlSession; static { try { // 1.第3步中数据库配置文件的路径 String resource = "mybatis-config.xml"; // 2.以数据流的形式获取到内容 InputStream inputStream = Resources.getResourceAsStream(resource); // 3.从XML配置文件构建SqlSessionFactory实例用于执行sql语句 sqlSession = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } // 4. 获得处理sql语句的对象SqlSession public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
*创建对应于表字段的实体类
public class User { private int id; private String name; private String pwd; public User(){} public User(int id, String name, String pwd) { this.id = id; this.name = name; this.pwd = pwd; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } }
*写dao层也就是mapper层,接口以及对应的实现类,接口还是照常写,但是之前的实现类为了java代码与sql语句分离,实现类用mapper.xml代替
import java.util.List; public interface UserMapper { List<User> getUserList(); }
可以在此目录下直接创建mapper.xml,但要注意,只有resource目录下的配置文件才能读取到,若在此目录下直接号才能构建mappper.xml,需要在maven父子项目中导入以下配置
<build> <!--扫描到java和resources下的xml等资源文件--> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> <include>**/*.xml</include> <include>**/*.tld</include> <include>**/*.properties</include> <!--properties文件--> </includes> <!--这里写成true会造成编译注释错误--> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> <!--properties文件--> </includes> <!--这里写成true会造成编译注释错误--> <filtering>false</filtering> </resource> </resources> </build>
*创建与接口对应的mapper.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"> <!-- namespace:绑定对应Mapper接口 --> <mapper namespace="com.test.dao.UserMapper"> <!-- id=接口方法名 resultType=接口返回的类型 --> <select id="getUserList" resultType="com.test.pojo.User"> select * from mybatis.user </select> </mapper>
*进行测试
public class UserMapperTest { @Test public void test(){ // 1.利用MybatisUtils获取执行sal的对象sqlSession SqlSession sqlSession = MybatisUtils.getSqlSession(); // 2.获取并执行sql语句 方法:getMapper UserMapper mapper = sqlSession.getMapper(UserMapper.class); //已经将sql查询出来的结果自动变为java对象了 List<User> userList = mapper.getUserList(); // 输出结果 for (User user : userList) { System.out.println(user); } // 3.关闭sqlSession连接 sqlSession.close(); } }