代码结构
image.png
Animal为实体类,Main测试方法
Main.java
package com.ly3;
import com.ly3.entity.Animal;
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 java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
public class Main {
public static void main(String[] args) throws IOException {
Properties properties=new Properties();
//加载配置文件
properties.load(Resources.getResourceAsStream("mappings3/sql2.properties"));
InputStream inputStream= Resources.getResourceAsStream("mappings3/mybatis-config-noresource.xml");
//创建一个SqlSessionFactory
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream,properties);
try(SqlSession sqlSession=sqlSessionFactory.openSession()) {
Animal animal1=new Animal();
int id=new Random().nextInt(1000000);
animal1.setAgeMax("14");
animal1.setType("兔子");
//使用xml的数据新增
sqlSession.insert("com.ly3.entity.AnimalMapper.insertAnimal", animal1);
animal1.setType(animal1.getType()+"[我被改了]");
//数据修改
sqlSession.update("com.ly3.entity.AnimalMapper.updateAnimal",animal1);
//数据查询
Animal animal=sqlSession.selectOne("com.ly3.entity.AnimalMapper.selectAnimal",animal1.getId());
Map<String,Integer> map=new HashMap<>();
map.put("id",animal1.getId()-1);
//数据删除
sqlSession.delete("com.ly3.entity.AnimalMapper.deleteAnimal",map);
System.out.println(animal);
//注意这里,增删改都是需要commit才会生效
sqlSession.commit();
}
}
}
mybatis-config-noresource.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>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<environments default="dv">
<environment id="dv">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="url" value="${url}"/>
<property name="driver" value="${driver}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mappings3/com/ly3/entity/AnimalMapper.xml" />
</mappers>
</configuration>
配置文件sql2.properties
url=jdbc:mysql://localhost:3306/test1
driver=com.mysql.cj.jdbc.Driver
username=root
password=test.lwm
AnimalMapper.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.ly3.entity.AnimalMapper">
<select id="selectAnimal" parameterType="int" resultType="com.ly3.entity.Animal">
SELECT * FROM animal where id = #{id}
</select>
<insert id="insertAnimal" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
insert into animal(`type`,age_max) values(#{type},#{ageMax})
</insert>
<update id="updateAnimal">
update animal set type = #{type} , age_max = #{ageMax} where id = #{id}
</update>
<delete id="deleteAnimal">
delete from animal where id = #{id}
</delete>
</mapper>
网友评论