handler 示例呢?
时间: 2023-07-07 22:14:05 浏览: 54
好的,下面给出一个Handler的示例。
首先,在主线程中创建Handler对象和子线程,并将Handler对象绑定到主线程的消息队列中:
```java
Handler handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
// 子线程执行完后的回调方法,在UI线程中执行
}
};
new Thread(new Runnable() {
@Override
public void run() {
// 子线程要执行的代码
Message message = Message.obtain();
handler.sendMessage(message);
}
}).start();
```
在子线程中执行完要执行的操作后,可以通过Handler的sendMessage方法向主线程传递一个空消息,然后在主线程的handleMessage方法中进行UI操作。
需要注意的是,在使用Handler时,必须将Handler对象绑定到主线程的消息队列中,这里使用了Looper.getMainLooper()方法来获取主线程的消息队列。另外,在子线程中不能直接进行UI操作,需要通过Handler将结果传递到主线程中进行处理。
相关问题
mybaits typehandler示例
以下是 MyBatis TypeHandler 的示例代码:
```java
import java.sql.*;
import org.apache.ibatis.type.*;
@MappedTypes(String.class)
public class ExampleTypeHandler extends BaseTypeHandler<String> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, encryptString(parameter));
}
@Override
public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
return decryptString(rs.getString(columnName));
}
@Override
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return decryptString(rs.getString(columnIndex));
}
@Override
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return decryptString(cs.getString(columnIndex));
}
private String encryptString(String plainText) {
// 这里是加密的代码
return plainText;
}
private String decryptString(String encryptedString) {
// 这里是解密的代码
return encryptedString;
}
}
```
这个 TypeHandler 会将插入数据库的字符串进行加密操作,查询时再解密操作。需要注意的是,这个示例仅供参考,加密的实现需要遵循相关的安全标准。
能给个示例吗?
好的,我可以给您提供一个简单的示例来演示如何在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类型字段。需要注意的是,这只是一个示例,实际应用中可能还需要进行更多的处理和优化。