maven mybatis 快速生成

1、添加pom.xml,获取依赖的jar包

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
com.bw30.aop
feg
war
0.0.1-SNAPSHOT
feg Maven Webapp
http://maven.apache.org





${basedir}/src/main/java
com.bw30.store.dao.mapper
com.bw30.store.dao.model.pojo

${basedir}/src/main/resources
com.bw30.store.dao.mapper


5.1.36
3.1.2
1.6
1.6






junit
junit
3.8.1
test


c3p0
c3p0
0.9.1.2


tk.mybatis
mapper
3.2.2


org.mybatis
mybatis-spring
1.2.3


org.mybatis
mybatis
3.3.0


com.github.pagehelper
pagehelper
4.0.1


tk.mybatis
mapper
3.1.2


log4j
log4j
1.2.17


org.slf4j
slf4j-api
1.7.12


joda-time
joda-time
2.8.2


commons-lang
commons-lang
2.6


org.projectlombok
lombok
1.16.6
runtime


javax.persistence
persistence-api
1.0


org.mybatis.generator
mybatis-generator-core
1.3.2


org.springframework
spring-context
4.2.1.RELEASE


org.springframework
spring-tx
4.2.1.RELEASE


org.springframework
spring-jdbc
4.2.1.RELEASE


mysql
mysql-connector-java
${mysql-connector-java.version}



feg


maven-compiler-plugin

${compiler-source.version}
${compiler-target.version}



org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2

${basedir}/src/main/resources/generatorConfig.xml
true
true



mysql
mysql-connector-java
${mysql-connector-java.version}


tk.mybatis
mapper
${mapper.version}






2.在src/main/resource 下面添加config.properties和generatorConfig.xml文件

config.properties 文件

mapper.plugin = tk.mybatis.mapper.generator.MapperPlugin

mapper.Mapper = tk.mybatis.mapper.common.Mapper

generatorConfig.xml 文件

 
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">





















enableSelectByExample="false" selectByExampleQueryId="false">




3 、写Student 类

package com.bw30.store.dao.model.pojo;


import javax.persistence.*;


import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@Table(name="Student")
public class Student {
    @Column(name="id")
    private String id;
    @Column(name="name")
    private String name;
    @Column(name="age")
    private Integer age;
    @Column(name="address")
    private String address;

}

4、运行maven

点击项目右键,选择Run,然后选择maven build .. ,输入mybatis-generator:generate

可以生成StudentMapper.java和StudentMapper.xml


5、spring-dao.xml 书写


xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  
    http://www.springframework.org/schema/mvc  
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-4.1.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">




classpath:/application-dao.properties









































 
dialect=mysql
pageSizeZero=true








 
mappers=tk.mybatis.mapper.common.Mapper
ORDER=AFTER












application-dao.properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://172.16.10.233:3306/test?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc.user=scal_store
jdbc.password=123456
jdbc.maxPoolSize=50
jdbc.minPoolSize=20
jdbc.initialPoolSize=1
jdbc.maxIdleTime=50
jdbc.checkoutTimeout=10000
jdbc.acquireRetryAttempts=3
jdbc.idleConnectionTestPeriod=50
jdbc.maxStatements=20


6、获取SqlSessionFactory

  String resource = "applicationContext-dao.xml";
       
       ClassPathXmlApplicationContext xmlApplicationContext = new ClassPathXmlApplicationContext(resource);
       
       sqlSessionFactory = xmlApplicationContext.getBean("sqlSessionFactory", SqlSessionFactory.class);

              SqlSession openSession = sqlSessionFactory.openSession();

StudentMapper mapper = openSession.getMapper(StudentMapper.class);
 
List selectByExample = mapper.selectByExample(new Example(Student.class));
System.out.println(selectByExample.size());


openSession.close();




你可能感兴趣的:(maven mybatis 快速生成)