主键返回
导入所需的包
创建工程并配置所需环境
<?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>
<typeAliases>
<typeAlias alias="User" type="com.tanlei.newer.pojo.User"></typeAlias>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8"></property>
<property name="username" value="root"></property>
<property name="password" value="password"></property>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/tanlei/newer/pojo/User.xml"></mapper>
</mappers>
</configuration>
创建 Java 类和接口及xml映射文件
JAVA类
package com.tanlei.newer.pojo; /** * @author:Mr.Tan * @Create:2018-10-31-16-05 **/ public class User { private int id; private String name; private String dept; private String phone; private String website; public String getWebsite() { return website; } public void setWebsite(String website) { this.website = website; } 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 getDept() { return dept; } public void setDept(String dept) { this.dept = dept; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
接口类
package com.tanlei.newer.pojo; import java.util.List; public interface IUser { //查找一个用户信息 public User getUser(int id); //增加一个用户 public void insertUser(User user); //删除一个用户 public void deleteUser(int userid); //修改一个用户 public void updateUser(User user); //查询所有用户 public 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"> <!--<mapper namespace="user">--> <mapper namespace="com.tanlei.newer.pojo.IUser"> <!--根据用户id查找一个用户信息--> <select id="getUser" parameterType="int" resultType="User"> SELECT * FROM USER WHERE id=#{userId} </select> <!--添加一个用户--> <insert id="insertUser" parameterType="User"> INSERT INTO USER(name,dept,phone,website) VALUES(#{name},#{dept},#{phone},#{website}); </insert> <!--根据id删除一个用户--> <delete id="deleteUser" parameterType="int"> DELETE FROM USER WHERE id = #{id} </delete> <!--修改用户信息--> <update id="updateUser" parameterType="User"> UPDATE USER SET name=#{name}, dept = #{dept}, website = #{website}, phone = #{phone} WHERE id =#{id} </update> <!--获取所有用户--> <select id="getUserList" resultType="User"> SELECT * FROM USER </select> </mapper>
main启动类方法
package com.tanlei.newer.test; import com.tanlei.newer.pojo.IUser; import com.tanlei.newer.pojo.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; import java.io.IOException; import java.io.Reader; /** * @author:Mr.Tan * @Create:2018-10-31-16-39 **/ public class main { private static Reader reader; private static SqlSessionFactory sqlSessionFactory; static{ try { reader= Resources.getResourceAsReader("config/Configure.xml"); sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader); } catch (IOException e) { e.printStackTrace(); } } public SqlSessionFactory getSession(){ return sqlSessionFactory; } public static void main(String[] args) { SqlSession sqlSession=sqlSessionFactory.openSession(); /*//根据id查找单个用户信息 User user = sqlSession.selectOne( "com.tanlei.newer.pojo.IUser.getUser",1); System.out.println(user.getName());*/ /*添加一个用户 insertUser();*/ /*删除用户 deleteUser();*/ /*根据id修改用户信息 updateUser(); */ /*获取用户列表 getUserList();*/ sqlSession.close();; } //添加一个用户 public static void insertUser(){ // 获取Session连接 SqlSession session = sqlSessionFactory.openSession(); // 获取Mapper IUser userMapper = session.getMapper(IUser.class); // 执行插入 User user = new User(); user.setName("Google"); user.setDept("Tech"); user.setWebsite("http://www.google.com"); user.setPhone("120"); userMapper.insertUser(user); // 提交事务 session.commit(); } //删除用户 public static void deleteUser(){ // 获取Session连接 SqlSession session = sqlSessionFactory.openSession(); // 获取Mapper IUser iuser = session.getMapper(IUser.class); iuser.deleteUser(1); session.commit(); } //根据id修改用户信息 public static void updateUser(){ // 获取Session连接 SqlSession session = sqlSessionFactory.openSession(); // 获取Mapper IUser iuser = session.getMapper(IUser.class); User user=iuser.getUser(1); user.setName("谭磊"); iuser.updateUser(user); session.commit(); } //查找所有用户信息 public static void getUserList(){ // 获取Session连接 SqlSession session = sqlSessionFactory.openSession(); // 获取Mapper IUser userMapper = session.getMapper(IUser.class); for(User user:userMapper.getUserList()){ System.out.println(user.getName() ); } session.commit(); } }