创建一个maven项目:
然后一路向下就行:
配置文件:
pom.xml
4.0.0
com.itheima
day11
1.0-SNAPSHOT
org.mybatis
mybatis
3.4.5
mysql
mysql-connector-java
8.0.18
log4j
log4j
1.2.12
junit
junit
4.12
test
**
**
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/eesy_mybatis?serverTimezone=UTC
jdbc.username=root
jdbc.password=123456
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
1、在main下的java文件夹下创建“包+user”
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer 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 + '\'' +
'}';
}
}
2、创建一个接口(包+IUserDao),为后面的动态代理:
package com.itheima.dao;
import com.itheima.domain.User;
import java.util.List;
public interface IUserDao {
/查询所有/
List findAll();
/保存用户信息/
void saveUser(User user);
/修改用户/
void updateUser(User user);
/根据id删除用户/
void deleteUser(Integer userId);
/根据id查询用户/
User findId(Integer userId);
/模糊查询/
List findByName(String username);
/聚合函数查询记录数/
int findTotal();
}
3、在resources下创建文件夹(三层)写一个IUserDao.xml文件:
/*单个查询一个用户的所有信息*/
/*后面的那个uid是随便写的*/
select *from user where id=#{uid};
/查询总的记录条数/
select count(*) from user;
4、在test文件夹下创建测试类:
package com.itheima.test;
import com.itheima.dao.IUserDao;
import com.itheima.domain.User;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
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.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
public class MapTest {
private InputStream in=null;
private SqlSession sqlSession=null;
IUserDao mapper;
//@Before用于在测试方法执行之前执行
@Before
public void init()throws IOException
{
in = Resources.getResourceAsStream(“SqlMapConfig.xml”);
//创建工厂来解析xml文件
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//执行包含SQL语句的方法
sqlSession = factory.openSession();
//创建代理对象
mapper = sqlSession.getMapper(IUserDao.class);
}
//用于在测试方法执行之后执行
@After
public void destory()throws IOException
{
/每次操作都会有一个事务的提交
* 如果提交失败事务会回滚/
sqlSession.commit();
//释放资源
if (sqlSession!=null){
sqlSession.close();}
if (in!=null){ in.close();}
}
@Test
public void fun() throws IOException
{
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
//创建工厂来解析xml文件
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//执行包含SQL语句的方法
SqlSession sqlSession = factory.openSession();
//创建代理对象
IUserDao mapper = sqlSession.getMapper(IUserDao.class);
//通过对象来执行查询操作
List userdao=mapper.findAll();
for (User user:userdao){
System.out.println(user);}
//释放资源
sqlSession.close();
in.close();
}
@Test
public void fun01()throws IOException
{
/*插入数据*/
User user=new User();
user.setUsername("高xx");
user.setAddress("xxxx");
user.setSex("女");
user.setBirthday(new Date());
//通过对象来执行查询操作
if (mapper!=null)
{
mapper.saveUser(user);
}
else {
System.out.println("mapper空指针");
}
}
@Ignore
public void fun04()throws IOException
{
/*删除数据*/
mapper.deleteUser(52);
}
@Ignore
public void fun03()throws IOException
{
/*插入数据*/
User user=new User();
user.setId(52);
user.setUsername("xxxx");
user.setAddress("xxxx");
user.setSex("男");
user.setBirthday(new Date());
//通过对象来执行查询操作
if (mapper!=null)
{
mapper.updateUser(user);
}
else {
System.out.println("mapper空指针");
}
}
@Test
public void fun05(){
/* 单个用户的查询*/
User user=mapper.findId(53);
System.out.println(user);
}
@Test
public void fun06()
{
List users = mapper.findByName("%王%");
for (User user:users)
{
System.out.println(user);
}
}
@Test
public void fun07(){
int total = mapper.findTotal();
System.out.println(total);
}
}
确实有点麻烦!!!!