目录结构
导入jar
- mybatis 依赖包
- mybatis包
- mysql包
Person.java
package com.baidu.demo1;
public class Person {
private int id;
private String username;
private String password;
private int sex;
private String address;
private String hobby;
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 String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public Person() {
super();
}
public Person(int id, String username, String password, int sex, String address, String hobby) {
super();
this.id = id;
this.username = username;
this.password = password;
this.sex = sex;
this.address = address;
this.hobby = hobby;
}
public Person(String username, String password, int sex, String address, String hobby) {
super();
this.username = username;
this.password = password;
this.sex = sex;
this.address = address;
this.hobby = hobby;
}
}
PersonDao.java
package com.baidu.demo1;
public interface PersonDao {
public void insert(Person person);
public Person select(int id);
public void update(Person person);
public void delete(int id);
}
PersonDaoImpl.xml
<mapper namespace="com.baidu.demo1.PersonDao">
<insert id="insert" parameterType="com.baidu.demo1.Person" >
insert into messages(id,username,password,sex,address,hobby) values(#{id},#{username},#{password},#{sex},#{address},#{hobby})
insert>
<select id="select" parameterType="int" resultType="com.baidu.demo1.Person" >
select * from messages where id=#{id}
select>
<update id="update" parameterType="com.baidu.demo1.Person">
update messages set username=#{username},password=#{password},sex=#{sex},address=#{address},hobby=#{hobby} where id=#{id}
update>
<delete id="delete" parameterType="int">
delete from messages where id=#{id}
delete>
mapper>
test测试类
package com.baidu.demo1;
import java.io.IOException;
import java.io.InputStream;
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.jupiter.api.Test;
import com.mysql.cj.protocol.ServerSession;
public class test {
@Test
public void test() {
SqlSession sqlSession = null;
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
sqlSession = sqlSessionFactory.openSession();
PersonDao personDao = sqlSession.getMapper(PersonDao.class);
Person p=new Person("小王", "passwd666", 1, "长春", "玩");
personDao.insert(p);
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
sqlSession.close();
}
}
@Test
public void test2(){
SqlSession sqlSession = null;
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
sqlSession = sqlSessionFactory.openSession();
PersonDao personDao = sqlSession.getMapper(PersonDao.class);
Person p=new Person(28,"xiao8","passwd",0,"jinan","play");
personDao.update(p);
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
sqlSession.close();
}
}
@Test
public void test3(){
SqlSession sqlSession = null;
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
sqlSession = sqlSessionFactory.openSession();
PersonDao personDao = sqlSession.getMapper(PersonDao.class);
personDao.delete(28);
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
sqlSession.close();
}
}
@Test
public void test4(){
SqlSession sqlSession = null;
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
sqlSession = sqlSessionFactory.openSession();
PersonDao personDao = sqlSession.getMapper(PersonDao.class);
Person p=personDao.select(1);
System.out.println(p.getId());
System.out.println(p.getUsername());
System.out.println(p.getPassword());
System.out.println(p.getSex());
System.out.println(p.getAddress());
System.out.println(p.getHobby());
sqlSession.commit();
} catch (IOException e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
sqlSession.close();
}
}
}
mybatis-config.xml
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/lyb?serverTimezone=UTC" />
<property name="username" value="root" />
<property name="password" value="123456" />
dataSource>
environment>
environments>
<mappers >
<mapper resource="com/baidu/demo/PersonDaoImpl.xml"/>
<mapper resource="com/baidu/demo1/UserDaoImpl.xml"/>
mappers>
configuration>