一、准备开发环境
安装jdk,下载一个eclipse,这里就不多讲啦
二、创建一个java项目,将下面的包导入到lib中并编译
具体怎么讲包导入并编译我这里稍微讲一下:选中项目右键——Build Path——Configure Build Path——Libraries
先添加jdk:
点击Finish 就讲jdk加到环境中了,然后添加jar包:
OK,到了这步运行环境已经搭建好了,那么开始操作吧。
三、创建sqlMapConfig.xml
首先你要在src下创建一个sqlMapConfig.xml文件,有人问了为啥叫这名?能改不?我可以告诉你这就是一种约定,就和hibernate的hibernate.cfg.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> <!-- <context:property-placeholder location="classpath:*.properties"/> --> <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.1.68:3306/test?characterEncoding=utf-8"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="cn/core/domain/User.xml" /> </mappers> </configuration>
四、数据库创建一张表(user,这里随便我演示所以叫user)
随便创建了4个字段:
四、创建User(实体类)对象
package cn.core.domain; import java.io.Serializable; import java.sql.Timestamp; /** * 用户实体类. * * @ClassName: User * @author [email protected] * @date: 2016年4月29日 上午8:22:09 * @version V1.0 */ public class User implements Serializable { //主键ID private String user_id; //用户的名字 private String uname; //地址 private String address; //年龄 private int age; public String getUser_id() { return user_id; } public void setUser_id(String user_id) { this.user_id = user_id; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User [user_id=" + user_id + ", uname=" + uname + ", address=" + address + ", age=" + age + "]"; } }
User
五、创建User.xml对象
注意这里创建User.xml就是在sqlMapConfig.xml中引入的节点,回顾一下:
<mappers> <mapper resource="cn/core/domain/User.xml" /> </mappers>
具体的User.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="cn.core.domain.User"> <!-- 查询单个对象 --> <select id="selectUserById" parameterType="string" resultType="cn.core.domain.User"> select * from user where user_id = #{user_id} </select> <!-- 添加 --> <insert id="insertUser" parameterType="cn.core.domain.User" > insert into user values(#{user_id},#{uname},#{address},#{age}) </insert> <!-- 修改 --> <update id="updateUser" parameterType="cn.core.domain.User"> update user set uname=#{uname},address=#{address},age=#{age} where user_id=#{user_id} </update> <!-- 删除 --> <delete id="deleteUser" parameterType="string" > delete from user where user_id=#{user} </delete> </mapper>
六、测试文件MyBitesTest.java文件
package cn.test; import java.io.IOException; import java.io.InputStream; import java.util.UUID; 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 org.junit.Before; import org.junit.Test; import cn.core.domain.User; public class MyBitesTest { SqlSessionFactory sf ; @Before public void initSessionFactory() throws Exception{ String resource = "sqlMapConfig.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); //获得会话工厂 sf = new SqlSessionFactoryBuilder().build(inputStream); } @Test public void selectUserById() throws IOException{ SqlSession session=sf.openSession(); User user=(User)session.selectOne("cn.core.domain.User.selectUserById", "1"); System.out.println(user); } @Test public void insertByUser() throws IOException{ SqlSession session=sf.openSession(); User user=new User(); user.setUser_id(UUID.randomUUID().toString()); user.setAge(33); user.setAddress("海淀区"); user.setUname("李四"); int num=session.insert("cn.core.domain.User.insertUser", user); System.out.println("受影响的行数:"+num); session.commit(); } @Test public void updateUserById(){ SqlSession session=sf.openSession(); User user=new User(); user.setUser_id("1"); user.setAge(12); user.setUname("王五"); user.setAddress("西城区"); int num=session.update("cn.core.domain.User.updateUser", user); System.out.println("受影响的行数:"+num); session.commit(); } @Test public void deleteUserById(){ SqlSession session=sf.openSession(); int num=session.delete("cn.core.domain.User.deleteUser", "1687bd8c-ee4a-4bc2-8c99-246cd08295cb"); System.out.println("受影响的行数:"+num); session.commit(); } }
注意:session对象下selectOne、insert、update、delete 等等 这些方法如果是2个参数的,第一个参数是你配置的xml中对应的命名空间+id名字,例如:User.xml的命名空间是cn.core.domain.User,对应的方法找对应的id,例如查询操作 这样表示:cn.core.domain.User.selectUserById
完整项目下载地址:http://pan.baidu.com/s/1qYrz7Ne
时间: 2024-11-07 10:03:39