Skip to content

Instantly share code, notes, and snippets.

@yterradas
Last active August 29, 2015 14:03
Show Gist options
  • Save yterradas/494bc99ce81fd5a139d8 to your computer and use it in GitHub Desktop.
Save yterradas/494bc99ce81fd5a139d8 to your computer and use it in GitHub Desktop.
spring-boot and mybatis working together, without the stinking help of mybatis-spring.
@Bean SqlSessionFactory sqlSessionFactory() {
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("dev", transactionFactory, dataSource());
Configuration configuration = new Configuration(environment);
// NOTE: you must register aliases before adding mappers.
configuration.getTypeAliasRegistry().registerAlias(MyClazz.class);
// NOTE: you can add a package where your mappers reside or add one at a time.
configuration.addMapper(IMyClazzMapper.class);
configuration.setMapUnderscoreToCamelCase(true);
return new SqlSessionFactoryBuilder().build(configuration);
}
public interface IClazzMapper {
MyClazz selectBySourceId( Integer sourceId );
}
<?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.mypackage.IMyClazzMapper">
<select id="selectById" resultType="MyClazz">
select id from my_table where id = #{id}
</select>
</mapper>
public class MyClazzDAO {
@Autowired SqlSessionFactory sessionFactory;
public MyClazz getById( final Integer Id ) {
MyClazz myClazz = null;
try (SqlSession session = sessionFactory.openSession()) {
IMyClazzMapper myClazzMapper = session.getMapper(IMyClazzMapper.class);
myClazz = myClazzMapper.selectById(id);
}
return myClazz;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment