Mybatis入门-例子

 

 

1.创建数据库表(User表)
复制代码
CREATETABLE `NewTable` (
`userId`
bigint(20) NOTNULL AUTO_INCREMENT ,
`userName`
varchar(30) CHARACTERSET utf8 COLLATE utf8_general_ci NOTNULL ,
`password`
varchar(80) CHARACTERSET utf8 COLLATE utf8_general_ci NOTNULL ,
`comment`
varchar(250) CHARACTERSET utf8 COLLATE utf8_general_ci NULLDEFAULTNULL ,
PRIMARYKEY (`userId`),
UNIQUEINDEX `UQ_User_userName` (`userName`) USING BTREE
)
复制代码

2.2. 创建实体类

复制代码
package com.mybatis.config;

publicclass User {
privateint userId;
private String userName;
private String password;
private String comment;
publicint getUserId() {
return userId;
}
publicvoid setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
publicvoid setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
publicvoid setPassword(String password) {
this.password = password;
}
public String getComment() {
return comment;
}
publicvoid setComment(String comment) {
this.comment = comment;
}
}
复制代码

3. 定义数据访问接口

复制代码
package com.mybatis.config;

import java.util.List;

publicinterface UserDao {

publicint insert(User user);

publicint update(User user);

publicint delete(String userName);

public List<User> selectAll();

publicint countAll();

public User findByUserName(String userName);
}
复制代码

4.创建MyBatis映射文件(UserDaoMapper.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="com.mybatis.config.UserDao">
<select id="countAll" resultType="int">
select count(*) c from user;
</select>

<select id="selectAll" resultType="com.mybatis.config.User">
select * from user order by userName asc
</select>

<insert id="insert" parameterType="com.mybatis.config.User">
insert into user(userName,password,comment) values(#{userName},#{password},#{comment})
</insert>

<update id="update" parameterType="com.mybatis.config.User">
update user set userName=#{userName},password=#{password},comment=#{comment} where userName=#{userName}
</update>

<delete id="delete" parameterType="int">
delete from user where userName=#{userName}
</delete>

<select id="findByUserName" parameterType="String" resultType="com.mybatis.config.User">
select * from user where userName=#{userName}
</select>
</mapper>
复制代码

5. 创建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>
<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://localhost:3306/hlp?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/mybatis/config/UserDaoMapper.xml"/>
</mappers>
</configuration>
复制代码

6. 编写测试代码

复制代码
package com.mybatis.config;

import java.io.Reader;
import java.util.Iterator;
import java.util.List;
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.Test;

publicclass UserDaoTest {
@Test
publicvoid userDaoTest() throws Exception
{
String resource
="MyBatis-Configuration.xml";
Reader reader
= Resources.getResourceAsReader(resource);
SqlSessionFactoryBuilder builder
=new SqlSessionFactoryBuilder();
SqlSessionFactory factory
= builder.build(reader);

SqlSession session
= factory.openSession();
UserDao userDao
= session.getMapper(UserDao.class);

User user
=new User();
user.setUserName(
"hongye");
user.setPassword(
"123456");
user.setComment(
"备注");

userDao.insert(user);
System.out.println(
"记录条数:"+userDao.countAll());

List
<User> users = userDao.selectAll();
Iterator
<User> iter = users.iterator();
while(iter.hasNext()){
User u
= iter.next();
System.out.println(
"用户名:"+u.getUserName()+"密码:"+u.getPassword());
}

user.setComment(
"comment");
userDao.update(user);
User u
= userDao.findByUserName("hongye");
System.out.println(u.getComment());

userDao.delete(
"hongye");
System.out.println(
"记录条数:"+userDao.countAll());

session.commit();
session.close();
}
}
posted @ 2013-11-16 22:27  ITren  阅读(258)  评论(0编辑  收藏  举报