Code Monkey home page Code Monkey logo

mybatis-jpa's Introduction

mybatis-jpa

1. mybatis-jpa 集成方式

1.1 配置文件

<!-- 在spring-mybatis配置文件中,增加以下配置即可.详见configs/spring-mybatis.xml -->
<!-- Mybatis JPA Mapper 所在包路径 -->
    <bean class="com.mybatis.jpa.core.MapperEnhancerScaner">
        <property name="basePackage" value="com.svili.mapper" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>

1.2 Entity示例

@Entity
/* {@Table}非必须,若无此注解,或其name="",将类名解析为下划线风格 做为表名 */
@Table(name = "user")
public class User {

    /* 非持久化字段 */
    @Transient
    private static final long serialVersionUID = -7788405797990662048L;

    /* {@Id}必须,主键标识,{@Column}非必须,若无此注解,或其name="",将字段名解析为下划线风格 做为SQL列名 */
    @Id
    @Column(name = "user_Id")
    private Integer userId;

    @Column(name = "password_alias")
    private String password;

    /* {@Enumerated}非必须,若无此注解,按照Mybatis约定,枚举类型使用{@EnumTypeHandler}解析 */
    @Enumerated
    @Column(name = "state")
    private DataStateEnum state;

    @Column(name = "create_Time")
    private java.util.Date createTime;

1.3 mapper示例

@Repository
@MapperDefinition(domainClass = User.class)
/*entends MybatisBaseMapper非必须,它只是定义了公共的方法签名,便于风格的统一*/
public interface UserMapper extends MybatisBaseMapper<User> {

    /* Like 的通配符需要自行添加 */
    @StatementDefinition
    List<User> selectByUserNameLike(String userName);

    @StatementDefinition
    List<User> selectByUserIdLessThan(Integer userId);

    @StatementDefinition
    List<User> selectByUserIdIsNull();

    /*more condition or complex SQL,need yourself build*/
    
    /**注意,此方法的resultMap是jpa自动生成的UserResultMap*/
    @Select("select * from user where user_name = #{userName} and dept_id = #{deptId}")
    @ResultMap(value="UserResultMap") 
    List<User> selectComplex(Map<String, Object> args); /*build with mapper.xml*/ List<User> selectComplex2(Map<String, Object> args);

2. 示例代码说明

测试代码在test目录
/test/resource 包含了spring + spring mvc + mybaits + jpa 的配置文件样例;
测试代码默认数据库为mysql,如需切换oracle,请在pom.xml中加入oracle ojdbc14依赖;

mybatis-jpa's People

Contributors

littlenewbie avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.