Mybatis Dao开发的两种方式(一)
原始Dao的开发方式:
1、创建数据库配置文件db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名称
jdbc.name=数据库登录用户名
jdbc.pwd=数据库登录密码
2、创建配置文件SqlMapConfig.xml
3、创建PO类
public class User {
private int id;
private String username;
private Date birthday;
private String sex;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + ''' +
", birthday=" + birthday +
", sex='" + sex + ''' +
", address='" + address + ''' +
'}';
}
}
4、创建映射文件
SELECT LAST_INSERT_ID()
INSERT INTO user(username,birthday,sex,address) VALUE (#{username},#{birthday},#{sex},#{address})
UPDATE user set birthday=#{birthday},sex=#{sex},address=#{address} WHERE id=#{id}
DELETE FROM user WHERE id=#{value}
5、在SqlMapConfig.xml中引入该映射文件
6、编写Dao接口文件
public interface UserServiceI{
//根据id查询用户的接口
public User findUserById(int id) throws Exception;
//根据用户名查询用户
public List
//插入用户信息
public int insertUserInfo(User user) throws Exception;
//根据id更新用户信息
public void updateUserInfo(User user) throws Exception;
//根据id删除用户信息
public void deleteUserInfo(int id) throws Exception;
}
7、编写接口实现类
public class UserServiceImpl implements UserServiceI{
private SqlSessionFactory sessionFactory;
public UserServiceImpl(SqlSessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}
@Override
public User findUserById(int id) throws Exception {
SqlSession session = sessionFactory.openSession();
User user = session.selectOne("findUserById",id);
return user;
}
@Override
public List
SqlSession session = sessionFactory.openSession();
List
return list;
}
@Override
public int insertUserInfo(User user) throws Exception {
SqlSession session = sessionFactory.openSession();
session.insert("insertUserInfo",user);
session.commit();
return user.getId();
}
@Override
public void updateUserInfo(User user) throws Exception {
SqlSession session = sessionFactory.openSession();
session.insert("updateUserInfo",user);
session.commit();
}
@Override
public void deleteUserInfo(int id) throws Exception {
SqlSession session = sessionFactory.openSession();
session.delete("deleteUserInfo",id);
session.commit();
}
}
8、编写测试代码进行测试
public class UserTest {
private SqlSessionFactory sessionFactory;
@Before
public void setUp() throws Exception{
String resource = "config/SqlMapConfig.xml";
InputStream in = Resources.getResourceAsStream(resource);
sessionFactory = new SqlSessionFactoryBuilder().build(in);
}
@Test
public void findUserById(){
UserServiceI userService = new UserServiceImpl(sessionFactory);
try {
User user = userService.findUserById(10);
System.out.println(user);
} catch (Exception e) {
e.printStackTrace();
}finally {
}
}
@Test
public void findUserByName(){
UserServiceI userService = new UserServiceImpl(sessionFactory);
try {
List
for (User user:list) {
System.out.println(user);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
}
}
@Test
public void insertUserInfo(){
UserServiceI userService = new UserServiceImpl(sessionFactory);
try {
User user = new User();
user.setId(26);
user.setBirthday(new Date());
user.setSex("1");
user.setAddress("甘肃天水");
userService.updateUserInfo(user);
} catch (Exception e) {
e.printStackTrace();
}finally {
}
}
public void updateUserInfo(){
UserServiceI userService = new UserServiceImpl(sessionFactory);
try {
User user = new User();
user.setUsername("东方不败");
user.setBirthday(new Date());
user.setSex("0");
user.setAddress("黑木崖");
int id =userService.insertUserInfo(user);
System.out.println(id);
} catch (Exception e) {
e.printStackTrace();
}finally {
}
}
@Test
public void deleteUserInfo(){
UserServiceI userService = new UserServiceImpl(sessionFactory);
try {
userService.deleteUserInfo(28);
} catch (Exception e) {
e.printStackTrace();
}finally {
}
}
}

浙公网安备 33010602011771号