<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="Encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" />
layout>
appender>
<logger name="java.sql">
<level value="debug" />
logger>
<logger name="org.apache.ibatis">
<level value="info" />
logger>
<root>
<level value="debug" />
<appender-ref ref="STDOUT" />
root>
log4j:configuration>
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
private Integer id;
private String empName;
private String email;
private String gender;
}
3.操作数据库的dao接口,这个接口中增删改查方法
public interface EmployeeDao {
public int insertEmployee(Employee employee);
public boolean deleteEmployee(int id);
public int updateEmployee(Employee employee);
public Employee getEmpById(int id);
}
1.写全局配置文件:mybatis-config.xml
<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/mybatis_01?
useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
dataSource>
environment>
environments>
configuration>
2.写映射配置文件:EmployeeDao.xml
<mapper namespace="com.hh.dao.EmployeeDao">
<select id="getEmpById" resultType="com.hh.bean.Employee">
select * from t_employee where id=#{id}
select>
<update id="updateEmployee">
update t_employee
set empname=#{empName},gender=#{gender},email=#{email} where id=#{id}
update>
<delete id="deleteEmployee">
delete from t_employee where id=#{id}
delete>
<insert id="insertEmployee">
insert into t_employee(empname,gender,email) values (#{empName},#{gender},#{email})
insert>
mapper>
3.将映射配置文件在全局配置文件中注册:
<mappers>
<mapper resource="com/hh/dao/EmployeeDao.xml"/>
mappers>
对于查询数据库中数据
public class MyTest {
SqlSessionFactory sqlsessionFactory;
@Before
public void initSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream =
Resources.getResourceAsStream(resource);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
sqlsessionFactory = builder.build(inputStream);
}
@Test
public void testSelect() throws IOException {
SqlSession sqlSession = sqlsessionFactory.openSession();
EmployeeDao mapper = sqlSession.getMapper(EmployeeDao.class);
Employee employee = mapper.getEmpById(1);
System.out.println(employee);
}
}
对于添加数据,删除数据,修改数据都需要手动提交commit
首先添加数据:
@Test
public void testInsert(){
SqlSession sqlSession = sqlsessionFactory.openSession();
try {
EmployeeDao mapper = sqlSession.getMapper(EmployeeDao.class);
int i = mapper.insertEmployee(
new Employee(null, "tomcat", "[email protected]", "0"));
System.out.println(i);
}finally {
//手动提交
sqlSession.commit();
sqlSession.close();
}
}
@Test
public void testUpdate(){
SqlSession sqlSession = sqlsessionFactory.openSession();
try {
EmployeeDao mapper = sqlSession.getMapper(EmployeeDao.class);
mapper.updateEmployee(new Employee(6,"gutianle","[email protected]","1"));
} finally {
//手动提交
sqlSession.commit();
sqlSession.close();
}
}
@Test
public void testDelete(){
SqlSession sqlSession = sqlsessionFactory.openSession();
try {
EmployeeDao mapper = sqlSession.getMapper(EmployeeDao.class);
mapper.deleteEmployee(6);
} finally {
//手动提交
sqlSession.commit();
sqlSession.close();
}
}