1.导入JAR包。
2.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" /> <!-- 配置数据库连接信息 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://192.168.0.20:3306/mybatis" /> <property name="username" value="root" /> <property name="password" value="71b386e372418369" /> </dataSource> </environment> </environments> <mappers> <!-- 注册userMapper.xml文件, userMapper.xml位于me.gacl.mapping这个包下,所以resource写成me/gacl/mapping/userMapper.xml--> <mapper resource="com/xxx/mapping/userMapper.xml"/> </mappers> </configuration>
3.userMapper.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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的 例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀) --> <mapper namespace="com.xxx.mapping.userMapper"> <!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复 使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型 resultType="me.gacl.domain.User"就表示将查询结果封装成一个User类的对象返回 User类就是users表所对应的实体类 --> <!-- 根据id查询得到一个user对象 --> <select id="getUser" parameterType="int" resultType="com.xxx.domain.User"> select * from users where id=#{id} </select> <!-- 创建用户(Create) --> <insert id="addUser" parameterType="com.xxx.domain.User"> insert into users(name,age) values(#{name},#{age}) </insert> <!-- 删除用户(Remove) --> <delete id="deleteUser" parameterType="int"> delete from users where id=#{id} </delete> <!-- 修改用户(Update) --> <update id="updateUser" parameterType="com.xxx.domain.User"> update users set name=#{name},age=#{age} where id=#{id} </update> <!-- 查询全部用户--> <select id="getAllUsers" resultType="com.xxx.domain.User"> select * from users </select> </mapper>
4.test1.java
package com.xxx.test; import java.io.IOException; import java.io.InputStream; import java.io.Reader; import com.xxx.domain.User; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class Test1 { public static void main(String[] args) throws IOException { //mybatis的配置文件 String resource = "conf.xml"; //使用类加载器加载mybatis的配置文件(它也加载关联的映射文件) InputStream is = Test1.class.getClassLoader().getResourceAsStream(resource); //构建sqlSession的工厂 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is); //使用MyBatis提供的Resources类加载mybatis的配置文件(它也加载关联的映射文件) //Reader reader = Resources.getResourceAsReader(resource); //构建sqlSession的工厂 //SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); //创建能执行映射文件中sql的sqlSession SqlSession session = sessionFactory.openSession(); /** * 映射sql的标识字符串, * me.gacl.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值, * getUser是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL */ String statement = "com.xxx.mapping.userMapper.getUser";//映射sql的标识字符串 //执行查询返回一个唯一user对象的sql User user = session.selectOne(statement, 1); System.out.println(user); } }
5.TestCRUDByXmlMapper.java
package com.xxx.test; import java.util.List; import com.xxx.domain.User; import com.xxx.domain.MyBatisUtil; import org.apache.ibatis.session.SqlSession; import org.junit.Test; public class TestCRUDByXmlMapper { @Test public void testAdd(){ //SqlSession sqlSession = MyBatisUtil.getSqlSession(false); SqlSession sqlSession = MyBatisUtil.getSqlSession(true); /** * 映射sql的标识字符串, * me.gacl.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值, * addUser是insert标签的id属性值,通过insert标签的id属性值就可以找到要执行的SQL */ String statement = "com.xxx.mapping.userMapper.addUser";//映射sql的标识字符串 User user = new User(); user.setName("用户孤傲苍狼"); user.setAge("20"); //执行插入操作 int retResult = sqlSession.insert(statement,user); //手动提交事务 //sqlSession.commit(); //使用SqlSession执行完SQL之后需要关闭SqlSession sqlSession.close(); System.out.println(retResult); } @Test public void testUpdate(){ SqlSession sqlSession = MyBatisUtil.getSqlSession(true); /** * 映射sql的标识字符串, * me.gacl.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值, * updateUser是update标签的id属性值,通过update标签的id属性值就可以找到要执行的SQL */ String statement = "com.xxx.mapping.userMapper.updateUser";//映射sql的标识字符串 User user = new User(); user.setId(3); user.setName("孤傲苍狼"); user.setAge("25"); //执行修改操作 int retResult = sqlSession.update(statement,user); //使用SqlSession执行完SQL之后需要关闭SqlSession sqlSession.close(); System.out.println(retResult); } @Test public void testDelete(){ SqlSession sqlSession = MyBatisUtil.getSqlSession(true); /** * 映射sql的标识字符串, * me.gacl.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值, * deleteUser是delete标签的id属性值,通过delete标签的id属性值就可以找到要执行的SQL */ String statement = "com.xxx.mapping.userMapper.deleteUser";//映射sql的标识字符串 //执行删除操作 int retResult = sqlSession.delete(statement,5); //使用SqlSession执行完SQL之后需要关闭SqlSession sqlSession.close(); System.out.println(retResult); } @Test public void testGetAll(){ SqlSession sqlSession = MyBatisUtil.getSqlSession(); /** * 映射sql的标识字符串, * me.gacl.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值, * getAllUsers是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL */ String statement = "com.xxx.mapping.userMapper.getAllUsers";//映射sql的标识字符串 //执行查询操作,将查询结果自动封装成List<User>返回 List<User> lstUsers = sqlSession.selectList(statement); //使用SqlSession执行完SQL之后需要关闭SqlSession sqlSession.close(); System.out.println(lstUsers); } }