发布于2021-03-10 18:54 阅读(554) 评论(0) 点赞(26) 收藏(3)
CREATE DATABASE `mybatis`; USE `mybatis`;
/* */ CREATE TABLE `user`( `id` INT(20) NOT NULL PRIMARY KEY, `name` VARCHAR(30) DEFAULT NULL, `pwd` VARCHAR(30) DEFAULT NULL )ENGINE = INNODB DEFAULT CHARSET = utf8; /* */ INSERT INTO `user` (`id`,`name`,`pwd`) VALUES (1,'汪瑞杰','123456'), (2,'汪瑞杰','528582'), (3,'汪瑞杰','286353');
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.perwrj</groupId> <artifactId>mybaits</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 导入依赖 --> <dependencies> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <!-- mybatis --> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>
<?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"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=ture&characterEncoding=UTF-8&serverTimezone=GMT%2B8"/> <property name="username" value="root"/> <property name="password" value="344165"/> </dataSource> </environment> </environments> <!-- 每一个Mapper.xml文件必须在mybaits核心配置文件中注册 --> <mappers> <mapper resource="com/perwrj/dao/UserMapper.xml"/> </mappers> </configuration>
package com.perwrj.utils; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; /** * (工厂模式)sqlSessionFactory --> sqlSession * @author wangruijie * */ public class MyBatisUtil { private static SqlSessionFactory sqlSessionFactory; static { try { //获取SqlSessionFactory对象 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { // TODO: handle exception } } // 既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。 // SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。 // 你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句 public static SqlSession getSqlSession() { return sqlSessionFactory.openSession(); } }
package com.perwrj.pojo; public class User { private int id; private String name; private String pwd; public User() { super(); // TODO Auto-generated constructor stub } public User(int id, String name, String pwd) { super(); 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 + "]"; } }
package com.perwrj.dao; import java.util.List; import com.perwrj.pojo.User; public interface UserMapper { List<User> getUserList(); }
<?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/Dao接口 --> <mapper namespace="com.perwrj.dao.UserMapper"> <!-- 查询 id=“重写的方法名” resultType=“返回值类型(全类名)” --> <select id="getUserList" resultType="com.perwrj.pojo.User"> select * from mybatis.user </select> </mapper>
package com.perwrj.dao; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import com.perwrj.pojo.User; import com.perwrj.utils.MyBatisUtil; public class UserMapperTest { @Test public void test() { //获得SqlSession对象, SqlSession sqlSession = MyBatisUtil.getSqlSession(); try { // 方式一:getMapper执行sql:更加简洁,类型更加安全,推荐使用 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> userList = userMapper.getUserList(); for (User user : userList) { System.out.println(user); } //方式一结束 // 方式二:老方法,不建议使用, /* * List<User> list = * sqlSession.selectList("com.perwrj.dao.UserMapper.getUserList"); * * for (User object : list) { System.out.println(object); } */ //方式二结束 }catch(Throwable e) { e.printStackTrace(); }finally { // 关闭sqlSession sqlSession.close(); } } }
org.apache.ibatis.binding.BindingException: Type interface com.perwrj.dao.UserMapper is not known to the MapperRegistry.(没有在mybatis-config.xml核心配置文件中注册Mapper)java.lang.ExceptionInInitializerError(初始化异常失败,没有找到UserMapper.xml文件)
2021-02-24
作者:Djfj
链接:http://www.javaheidong.com/blog/article/112363/eec28b5763b82cbeab76/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!