org.apache.ibatis Test
mybatis-config.xml
<?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.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/zwf?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="zwf"/>
<property name="password" value="13805029595"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="StudentMapper.xml" /> <!-- Or use annotation-based mapper -->
</mappers>
</configuration>
StudentMapper.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.hz.nft.trade.admin.zwf.StudentMapper">
<!-- Example SQL statements (adapt as needed) -->
<insert id="insert" parameterType="com.hz.nft.trade.admin.zwf.Student">
INSERT INTO student (code, name) VALUES (#{code}, #{name})
</insert>
<delete id="deleteById" parameterType="String">
DELETE FROM student WHERE code = #{code}
</delete>
<update id="updateById" parameterType="com.hz.nft.trade.admin.zwf.Student">
UPDATE student SET name = #{param1.name} WHERE code = #{param1.code}
</update>
<select id="selectById" parameterType="String" resultType="com.hz.nft.trade.admin.zwf.Student">
SELECT code, name FROM student WHERE code = #{code}
</select>
<select id="selectAll" resultType="com.hz.nft.trade.admin.zwf.Student">
SELECT code, name FROM student
</select>
<select id="selectList" resultType="com.hz.nft.trade.admin.zwf.Student">
SELECT code, name FROM student
</select>
</mapper>
Student.java
package com.hz.nft.trade.admin.zwf;
public class Student
{
private String code;
private String name;
public String getCode()
{
return code;
}
public void setCode(String code)
{
this.code = code;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
StudentMapper.java
package com.hz.nft.trade.admin.zwf;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface StudentMapper extends BaseMapper<Student>
{
//You can add custom methods here if needed, beyond the BaseMapper's built-in methods.
List<Student> selectAll();
}
IbatisTest.java
package com.hz.nft.trade.admin.zwf;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.SqlSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class IbatisTest
{
public static void main(String[] args) throws IOException
{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession(true);// autoCommit=true
try
{
StudentMapper studentMapper = session.getMapper(StudentMapper.class);
// 删除
studentMapper.deleteById("005129");
// 新增
Student student = new Student();
student.setCode("005129");
student.setName("ZZZZ");
studentMapper.insert(student);
// 根据ID查询
Student selectedStudent = studentMapper.selectById("005129");
System.out.println("Selected Student: " + selectedStudent);
// 查询
// List<Student> allStudents = studentMapper.selectAll(); //Custom method, or use BaseMapper's selectList(null)
List<Student> allStudents = studentMapper.selectList(null); //Custom method, or use BaseMapper's selectList(null)
System.out.println("All Students: " + allStudents);
// 修改
student.setName("ZWf");
studentMapper.updateById(student);
// 删除
studentMapper.deleteById("005129");
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (session != null)
{
session.close();
}
}
}
}