您的位置:首页 > 其它

mybitis学习笔记

2015-12-09 17:30 239 查看
<?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="com.fxr.mapper.StudentMapper">

<!-- 中间,对象的属性和结果集的字段的对应的关系 -->
<resultMap type="com.fxr.model.Student" id="studentRM">
<!-- 主键的映射 -->
<id property="id" column="ID"/>
<!-- 普通字段的映射 property指的是实体的属性;column结果集中的字段的名称 -->
<result property="name" column="NAME"/>
<result property="age" column="AGE"/>
<result property="sex" column="SEX"/>
<result property="birthday" column="BIRTHDAY"/>
<!-- 对象关联 -->
</resultMap>

<!-- 查询,注意Mybatis中如果有填写集合的类型,只填写集合中元素的类型 -->
<select id="find" resultMap="studentRM">
select ID,NAME,AGE,SEX,BIRTHDAY from student
</select>

<!-- 查询一个按照id查询 -->
<select id="get" parameterType="int" resultType="com.fxr.model.Student">
select * from student where id = #{id}
</select>

<!-- 添加 -->
<insert id="insert" parameterType="com.fxr.model.Student">
insert into student
(id,name,age,sex,birthday)values
(#{id},#{name},#{age},#{sex},#{birthday})
</insert>

<!-- 修改 -->
<update id="update" parameterType="com.fxr.model.Student">
update student set name=#{name},age=#{age},birthday = #{birthday},sex=#{sex}
where id = #{id}
</update>

<!-- 删除一条 -->
<delete id="deleteById" parameterType="int">
delete from student where id=#{id}
</delete>

<!-- 删除多条整型数组 -->
<delete id="deleteArray" parameterType="int">
delete from student where id in
<foreach collection="array" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>

<!-- 删除多条list集合 -->
<delete id="deleteList" parameterType="int">
delete from student where id in
<foreach collection="list" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>

<!-- 删除多条,map,ids为map中的值 -->
<delete id="deleteMap" parameterType="map">
delete from student
where id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>

</mapper>

public class Student {

private Integer id;
private String name;
private String sex;
private Integer age;
private Date birthday;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", sex=" + sex
+ ", age=" + age + ", birthday=" + birthday + "]";
}

}

public class TestMyBatis {

private SqlSessionFactory factory;
@Before
public void init() throws IOException{
String resource = "mybatis-config.xml";
InputStream is = Resources.getResourceAsStream(resource);
factory = new SqlSessionFactoryBuilder().build(is);
}

//@Test
// public void testInsert(){
// SqlSession session = factory.openSession();
// Student s = new Student();
// s.setAge(10);
// s.setBirthday(new Date());
//
// s.setName("樊西蕊");
// s.setSex("男");
//
// session.insert("com.fxr.mapper.StudentMapper.insert", s);
// session.commit();//提交事务
// System.out.println("insert finished.");
//
// }

@Test
public void testFindAll(){
SqlSession session = factory.openSession();
//如何访问mapper中的方法啊,规则就是命名空间+.+id
List<Student> personList = session.selectList("com.fxr.mapper.StudentMapper.find");
System.out.println(personList.size());
for (Student s:personList) {
System.out.println(s.toString());
}
}

// @Test
// public void testGet(){
// SqlSession session = factory.openSession();
// Student s = session.selectOne("com.fxr.mapper.StudentMapper.get","1");
// System.out.println("id:"+s.getId()+s.toString());
// }

// @Test
// public void testUpdate(){
// SqlSession session = factory.openSession();
// Student s = new Student();
// s.setId(1);
// s.setName("张三");
// session.update("com.fxr.mapper.StudentMapper.update",s);
// session.commit();
// }
//删除一条数据
// @Test
// public void testDeleteById(){
// SqlSession session = factory.openSession();
// session.delete("com.fxr.mapper.StudentMapper.deleteById",4);
// session.commit();
// }

//删除多条数据
// @Test
// public void testDeleteByArray(){
// SqlSession session = factory.openSession();
// int [] ids = {5,7};
// session.delete("com.fxr.mapper.StudentMapper.deleteArray",ids);
// session.commit();
// }

//删除多条-List
// @Test
// public void testDeleteList(){
// SqlSession session = factory.openSession();
// List<Integer> list = new ArrayList<Integer>();
// list.add(3);
// list.add(8);
// session.delete("com.fxr.mapper.StudentMapper.deleteList",list);
// session.commit();
//
// }

//删除多条-map
@Test
public void testDeleteMap(){
SqlSession session = factory.openSession();
Map<String,Object> paraMap = new HashMap<String,Object>();
int [] ids = {2,6};
paraMap.put("ids", ids);
session.delete("com.fxr.mapper.StudentMapper.deleteMap",paraMap);
session.commit();

}

}
<?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="test">

<environment id="test">
<!-- 事务:JDBC/MANGED自己管理去 -->
<transactionManager type="JDBC"/>
<!-- 数据源:POOLED/UNPOOLED/JNDI -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatisdb?charsetEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>

<environment id="deploy">
<!-- 事务:JDBC/MANAGED-自己管理去 -->
<transactionManager type="JDBC"/>
<!-- 数据源:POOLED/UNPOOLED/JNDI -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatisdb?charsetEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>

</environments>

<!-- 映射文件mapper -->
<mappers>
<mapper resource="com/fxr/mapper/StudentMapper.xml"/>
</mappers>

</configuration>

/firstMyBatis/lib/asm-3.3.1.jar
/firstMyBatis/lib/cglib-2.2.2.jar
/firstMyBatis/lib/commons-logging-1.1.1.jar
/firstMyBatis/lib/javassist-3.17.1-GA.jar
/firstMyBatis/lib/log4j-1.2.17.jar
/firstMyBatis/lib/mybatis-3.2.2.jar
/firstMyBatis/lib/mysql-connector-java-5.1.26.jar
/firstMyBatis/lib/slf4j-api-1.7.5.jar
/firstMyBatis/lib/slf4j-log4j12-1.7.5.jar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: