//接口内容以及注意事项
1 package cn.jy.mybatis.mapper; 2 3 import java.util.List; 4 5 import cn.jy.mybatis.pojo.User; 6 7 public interface UserMapper { 8 //四个原则 9 //1 .xml中的名称空间要绑定此接口 <mapper namespace="cn.jy.mybatis.mapper.UserMapper"> 10 //2 接口的方法名必须跟 .xml文件的id名一致 11 //3 接口中的方法的返回值类型要跟 .xml返回值类型一致 12 //4 接口中的方法的参数类型要跟 .xml文件的传入的参数类型一致 13 14 User findUserById(int id); 15 List<User> findUserByUserName(String username); 16 void insertUser(User u); 17 void updateUserById(User u); 18 void deleteUserById(int u); 19 }
//.xml 配置文件
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper
3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5 <!-- sql语句 --> 6 <mapper namespace="cn.jy.mybatis.mapper.UserMapper"> 7 <!-- 通过id 查找用户 --> 8 <select id="findUserById" parameterType="Integer" resultType="cn.jy.mybatis.pojo.User"> 9 select * from user where id=#{qiang} 10 </select> 11 <!-- 模糊查询 --> 12 <select id="findUserByUserName" parameterType="String" resultType="cn.jy.mybatis.pojo.User"> 13 select * from user where username like "%"#{qaingtong}"%" 14 </select> 15 <!--添加用户 --> 16 <insert id="insertUser" parameterType="cn.jy.mybatis.pojo.User" > 17 insert into user (username,birthday,address,sex)values (#{username},#{birthday},#{address},#{sex}) 18 </insert> 19 <!-- 更新 --> 20 <update id="updateUserById" parameterType="cn.jy.mybatis.pojo.User"> 21 update user 22 set username=#{username},sex=#{sex},birthday=#{birthday},address=#{address} 23 where id=#{id} 24 </update> 25 <!-- 删除 --> 26 <delete id="deleteUserById" parameterType="Integer"> 27 delete from user 28 where id=#{DDDD} 29 </delete> 30 </mapper>
//sqlMapConfig.xml主配置文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 7 <!-- 别名 包以其子包下所有类 头字母大小都行--> 8 <typeAliases> 9 <!-- <typeAlias type="cn.lijun.mybatis.pojo.User" alias="User"/> --> 10 <package name="cn.jy.mybatis.pojo"/> 11 </typeAliases> 12 <!-- 和spring整合后 environments配置将废除 --> 13 <environments default="development"> 14 <environment id="development"> 15 <!-- 使用jdbc事务管理 --> 16 <transactionManager type="JDBC" /> 17 <!-- 数据库连接池 --> 18 <dataSource type="POOLED"> 19 <property name="driver" value="com.mysql.jdbc.Driver" /> 20 <property name="url" 21 value="jdbc:mysql://localhost:3306/day58?characterEncoding=utf-8" /> 22 <property name="username" value="root" /> 23 <property name="password" value="root" /> 24 </dataSource> 25 </environment> 26 </environments> 27 <mappers> 28 <mapper resource="sqlMap/User.xml"/> <!-- 上一篇所用到的--> 29 <mapper resource="cn/jy/mybatis/mapper/UserMapper.xml"/> 30 </mappers> 31 </configuration>
//测试类
1 package cn.jy.mybatis.mapper; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.util.Date; 6 import java.util.List; 7 8 import org.apache.ibatis.io.Resources; 9 import org.apache.ibatis.session.SqlSession; 10 import org.apache.ibatis.session.SqlSessionFactory; 11 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 12 import org.junit.Test; 13 14 import cn.jy.mybatis.pojo.User; 15 16 public class MybatisMapperTest { 17 @Test 18 public void TestMapper() throws IOException{ 19 String re="sqlMapConfig.xml"; 20 InputStream resourceAsStream = Resources.getResourceAsStream(re); 21 SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream); 22 SqlSession openSession = build.openSession(); 23 24 //帮助我们给接口创建实现类 25 UserMapper mp=openSession.getMapper(UserMapper.class); 26 27 //1 根据id查找 28 // User findUserById = mp.findUserById(22); 29 // System.out.println(findUserById); 30 31 32 //2 根据名字模糊查找 33 // List<User> findUserByUserName = mp.findUserByUserName("小明"); 34 // for(User a:findUserByUserName){ 35 // System.out.println(a); 36 // } 37 //3 添加操作 38 // User user = new User(); 39 // user.setAddress("北京"); 40 // user.setBirthday(new Date()); 41 // user.setSex("男"); 42 // user.setUsername("殷總"); 43 // mp.insertUser(user); 44 // openSession.commit(); 45 46 //4 更新操作 47 User user = new User(); 48 user.setAddress("北京"); 49 user.setBirthday(new Date()); 50 user.setId(31); 51 user.setSex("男"); 52 user.setUsername("王总666"); 53 mp.updateUserById(user); 54 openSession.commit(); 55 //刪除操作 56 // User user = new User(); 57 // mp.deleteUserById(26); 58 // openSession.commit(); 59 } 60 }
//文件位置关系