直接上代码,全部在代码里讲解。
1.实体类
package com.home.entity; /** * 此类是:user实体类 * @author hpc * @2017年1月10日下午9:36:59 */ public class User { private Integer user_id; private String user_name; private String user_pwd; public User(Integer user_id, String user_name, String user_pwd) { super(); this.user_id = user_id; this.user_name = user_name; this.user_pwd = user_pwd; } public Integer getUser_id() { return user_id; } public void setUser_id(Integer user_id) { this.user_id = user_id; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getUser_pwd() { return user_pwd; } public void setUser_pwd(String user_pwd) { this.user_pwd = user_pwd; } @Override public String toString() { return "User [user_id=" + user_id + ", user_name=" + user_name + ", user_pwd=" + user_pwd + "]"; } }
2. 接口注解
package com.home.entityInterface; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import com.home.entity.User; /** * 此类是:映射关系接口 * @author hpc * @2017年1月11日下午7:09:20 */ public interface UserMapperInterface{ /** * 获取指定id的User * 当我们调用这个方法的时候就会去执行这一条sql语句, * 这里面要注意点的是: * 传进来的参数是以对象的形式传进来的, * 所以取的时候要以对象属性来取,否则将会报错. */ @Select("select * from users where user_id=#{user_id}") public User loadUser(User user); /** * 更新user */ @Update("update users set user_name=#{user_name},user_pwd=#{user_pwd} where user_id=#{user_id}") public int updateUser(User user); /** * 删除user */ @Delete("delete from users where user_id=#{user_id}") public int deleteUser(User user); /** * 插入user */ @Insert("insert into users(user_name,user_pwd) values(#{user_name},#{user_pwd}) ") public int insertUser(User user); }
3.mybatis的配置文件(要和xml配置一样,都要在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><!-- 这是mybatis的配置,我们的配置都写在这里面 --> <properties resource="jdbc.properties"/> <!--properties标签用于 引入properties文件,在下面属性中我们要引用到properties中的内容 --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/><!-- 事务管理 --> <dataSource type="POOLED"><!-- 数据源 --> <property name="driver" value="${jdbc.driverClass}"/> <!-- 指定哪一个驱动 ,从properties中引用--> <property name="url" value="${jdbc.url}"/> <!-- 指定哪一个链接url --> <property name="username" value="${jdbc.user}"/> <!-- 指定哪一个链接用户 --> <property name="password" value="${jdbc.password}"/> <!-- 指定哪一个链接密码 --> </dataSource> </environment> </environments> <!-- 注册映射关系文件 --> <mappers> <!-- 这是我们上次注册映射关系配置文件的方式,这是以xml文件配置的方式注册的 --> <!-- <mapper resource="com/home/entity/userMapper.xml"/> --> <!-- 这次我们以接口的方式注入 ,所有的sql语句已经在接口的方法上写好了,就没必要在写 配置文件了--> <mapper class="com.home.entityInterface.UserMapperInterface"/> </mappers> </configuration>
4.测试
package com.home.mybatis; import java.io.IOException; import org.apache.ibatis.session.SqlSession; import com.home.entity.User; import com.home.entityInterface.UserMapperInterface; import com.home.mybatis.utils.SessionUtils; public class TestApp { public static void main(String[] args) throws IOException { // 我将获取session的代码封装成了一个工具类,直接用工具类来获取session SqlSession session = SessionUtils.getSession("mybatis.xml"); // 通过接口,直接使用session.getMapper(接口.class);获取到接口 UserMapperInterface mapper = session.getMapper(UserMapperInterface.class); // 再调用接口的方法,去执行接口所注解的sql语句 System.out.println(mapper.insertUser(new User(null,"mh", "123"))); System.out.println(mapper.updateUser(new User(3, "hpc", "123"))); System.out.println(mapper.deleteUser(new User(3, null, null))); System.out.println(mapper.loadUser(new User(1, null, null))); // 将数据属性到数据中去 session.commit(); // 关闭session session.close(); } }
5.结果