第一个Mybatis程序
Posted 可乐乐乐乐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第一个Mybatis程序相关的知识,希望对你有一定的参考价值。
代码演示:
1、搭建数据库环境:
CREATE DATABASE mybatis; USE mybatis; DROP TABLE IF EXISTS user; CREATE TABLE user ( id int(20) NOT NULL, name varchar(30) DEFAULT NULL, pwd varchar(30) DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; insert into user(id,name,pwd) values (1,\'黑子\',\'123456\'),(2,\'美琴\',\'abcdef\'),(3,\'初春\',\'987654\');
2、导入MyBatis相关的包
<!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency>
导入log4j测试包:
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
3、编写Mybatis核心配置文件
<?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?useSSL=true&useUnicode=true&characterEncoding=utf8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <mapper class="com.ttt.dao.UserMapper"/> </mappers> </configuration>
4、编写Mybatis工具类
public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; static{ String str = "mybatis_config.xml"; InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream(str); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
5、编写实体类
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 + \'\\\'\' + \'}\'; } }
6、编写mapper接口
public interface UserMapper { //获取全部用户 List<User> selectUser(); //根据id查询用户 User getUserByID(int id); //insert一个用户 int addUser(User user); //修改用户 int updateUser(User user); //删除一个用户 int deleteUser(int id); }
7、编写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"> <mapper namespace="com.UserMapper"> <select id="selectUser" resultType="com.ttt.pojo.User"> select * from mybatis.user </select> <select id="getUserByID" parameterType="int" resultType="com.ttt.pojo.User"> select * from mybatis.user where id=${id} </select> <insert id="addUser" parameterType="com.ttt.pojo.User"> insert into user(id, name, pwd) values (#{id}, #{name}, #{pwd}) </insert> <update id="updateUser" parameterType="com.ttt.pojo.User"> update user set name = #{name}, pwd=#{pwd} where id=#{id}; </update> <delete id="deleteUser" parameterType="int"> delete from mybatis.user where id=#{id}; </delete> </mapper>
8、编写测试类:
- Junit 包测试(测试查询功能)
@Test public void test01(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> users = userMapper.selectUser(); for (User user : users) { System.out.println(user); } sqlSession.close(); }
9、测试结果:
Maven静态资源过滤问题:
通过问题提示:Could not find resource com/ttt/dao/UserMapper.xml,而我的UserMapper接口与UserMapper配置文件都放在一起,在Mybatis的核心配置文件配置的mapper路径也没有问题。导致这个问题是由于Maven静态资源过滤,就是Maven项目编译java文件中只能编译java文件,而配置文件被过滤出来没有编译,可以看最终target文件:
就是最终只有UserMapper.class文件。
解决方法:在局部或者全局的pom.xml添加:
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
以上是关于第一个Mybatis程序的主要内容,如果未能解决你的问题,请参考以下文章