MybatisUtil示例

package cn.bdqn.util;

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;

public class MybatisUtil {

    private static SqlSessionFactory factory;
    static{
        String configName="mybatis-config.xml";
        InputStream in;
        try {
            
            in=Resources.getResourceAsStream(configName);
            factory=new SqlSessionFactoryBuilder().build(in);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    //获取SqlSession
    public static SqlSession getSession(){
        
        
        return factory.openSession();        
        
    }
    //关闭SqlSession
    public static void closeSession(SqlSession session){
        if(session!=null){
            session.close();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以给您提供一个简单的示例来演示如何在Java中使用JTS库和MyBatis操作MySQL中的Geometry类型字段。 首先,我们需要在MySQL中创建一个包含Geometry类型字段的表,例如: ```sql CREATE TABLE `my_table` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) DEFAULT NULL, `location` geometry DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ``` 然后,我们可以使用JTS库来创建一个Point类型的Geometry对象,例如: ```java import com.vividsolutions.jts.geom.GeometryFactory; import com.vividsolutions.jts.geom.Point; // 创建一个GeometryFactory对象 GeometryFactory gf = new GeometryFactory(); // 创建一个Point对象 Point point = gf.createPoint(new Coordinate(116.380943, 39.919145)); ``` 接下来,我们可以使用MyBatis的Mapper来进行数据的增删改查操作。首先,需要定义一个Mapper接口和相应的XML文件,例如: ```java public interface MyTableMapper { void insert(MyTable record); void update(MyTable record); void deleteById(Integer id); MyTable selectById(Integer id); } ``` ```xml <mapper namespace="com.example.mapper.MyTableMapper"> <resultMap id="myTableResultMap" type="com.example.entity.MyTable"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="location" property="location" jdbcType="OTHER" javaType="com.vividsolutions.jts.geom.Geometry" typeHandler="com.example.handler.GeometryTypeHandler"/> </resultMap> <insert id="insert" parameterType="com.example.entity.MyTable"> INSERT INTO my_table(name, location) VALUES (#{name}, ST_GeomFromText(#{location}, 4326)) </insert> <update id="update" parameterType="com.example.entity.MyTable"> UPDATE my_table SET name=#{name}, location=ST_GeomFromText(#{location}, 4326) WHERE id=#{id} </update> <delete id="deleteById" parameterType="int"> DELETE FROM my_table WHERE id=#{id} </delete> <select id="selectById" resultMap="myTableResultMap" parameterType="int"> SELECT id, name, ST_AsText(location) as location FROM my_table WHERE id=#{id} </select> </mapper> ``` 在XML文件中,我们需要使用自定义的TypeHandler来将Geometry类型字段转换为字符串类型进行存储和查询,例如: ```java import com.vividsolutions.jts.geom.Geometry; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.TypeHandler; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class GeometryTypeHandler implements TypeHandler<Geometry> { @Override public void setParameter(PreparedStatement ps, int i, Geometry parameter, JdbcType jdbcType) throws SQLException { ps.setString(i, parameter.toText()); } @Override public Geometry getResult(ResultSet rs, String columnName) throws SQLException { return new WKTReader().read(rs.getString(columnName)); } @Override public Geometry getResult(ResultSet rs, int columnIndex) throws SQLException { return new WKTReader().read(rs.getString(columnIndex)); } @Override public Geometry getResult(CallableStatement cs, int columnIndex) throws SQLException { return new WKTReader().read(cs.getString(columnIndex)); } } ``` 最后,我们可以使用Mapper接口来进行数据的操作,例如: ```java import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import java.io.IOException; // 获取SqlSessionFactory对象 SqlSessionFactory sessionFactory = MyBatisUtil.getSqlSessionFactory(); // 创建一个MyTable对象 MyTable myTable = new MyTable(); myTable.setName("test"); myTable.setLocation(point); // 获取SqlSession对象 SqlSession session = sessionFactory.openSession(); try { // 获取MyTableMapper对象 MyTableMapper mapper = session.getMapper(MyTableMapper.class); // 插入数据 mapper.insert(myTable); // 更新数据 myTable.setName("test2"); myTable.setLocation(point); mapper.update(myTable); // 查询数据 MyTable result = mapper.selectById(myTable.getId()); System.out.println(result.getId()); System.out.println(result.getName()); System.out.println(result.getLocation()); } finally { session.close(); } ``` 以上就是一个简单的示例,演示了如何在Java中使用JTS库和MyBatis操作MySQL中的Geometry类型字段。需要注意的是,这只是一个示例,实际应用中可能还需要进行更多的处理和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值