ssm
2017-08-21 本文已影响0人
无聊新生
mybatis逆向工程
逆向工程配置文件
generatorConfig.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="testTables" targetRuntime="MyBatis3">
<!-- 是否去掉注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="驱动类"
connectionURL="连接地址" userId="用户名"
password="密码">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL
和 NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成POJO类的位置 -->
<javaModelGenerator targetPackage="生成POJO类的位置"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="生成mapper映射文件的位置"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="mapper接口生成的位置" targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<!-- tableName:表名
domainObjectName:生成java对象的类名
-->
<table tableName="user"
domainObjectName="User"
enableUpdateByExample="false"
enableDeleteByExample="false">
</table>
<table tableName="app_version"
domainObjectName="AppVersion"
enableUpdateByExample="false"
enableDeleteByExample="false">
</table>
<table tableName="app_info"
domainObjectName="AppInfo"
enableUpdateByExample="false"
enableDeleteByExample="false">
</table>
<table tableName="app_category"
domainObjectName="AppCategory"
enableUpdateByExample="false"
enableDeleteByExample="false">
</table>
</context>
</generatorConfiguration>
逆向工程代码
public class GeneratorSqlmap {
public void generator() throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
// 指定逆向工程配置文件
File configFile = new File("generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
public static void main(String[] args) throws Exception {
try {
GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
测试类(可以在ssm整合的时候测试)
public static void main(String[] args) throws IOException {
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
SqlSession session = factory.openSession();
UserMapper mapper = session.getMapper(UserMapper.class);
UserExample example=new UserExample();
example.createCriteria().andUserNameEqualTo("dev2").andUserPwdEqualTo("123");
List<User> list = mapper.selectByExample(example);
System.out.println(list);
}
spring-mybatis整合
目录结构
1.png1、spring-mybatis整合xml文件
applicationContext-mybaties.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:properties/db.properties"/>
<!-- 配置数据 -->
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置SqlSessionFactoryBean:用来创建SqlSessionFacotry,并且扫描xml文件-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFacotry">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--配置别名,如果在下面的mybatis配置文件中有配置别名,那么这句可以不用-->
<!-- <property name="typeAliasesPackage" value="com.hemi.bean"></property> -->
<!--加载mybatis配置文件-->
<property name="configLocation" value="classpath:spring/mybatis-config.xml"></property>
<!-- 扫描mapper.xml -->
<property name="mapperLocations" value="classpath:/mapper/*.xml"></property>
</bean>
<!-- 配置MapperScannerConfigurer:用来扫描mapper接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描mapper接口 -->
<property name="basePackage" value="com.hemi.dao"/>
<!--注入sqlSessionFacotry-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFacotry"></property>
</bean>
</beans>
2、mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="autoMappingBehavior" value="FULL"/>
</settings>
</configuration>
3、db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/appmanager?useUnicode=true&characterEncoding=utf-8
jdbc.user=root
jdbc.password=
jdbc.acquireIncrement=3
jdbc.initialPoolSize=10
jdbc.minPoolSize=2
jdbc.maxPoolSize=200
jdbc.maxIdleTime=1000
4、log4j.properties
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.hemi.controller=DEBUG
log4j.logger.com.hemi.dao=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
spring事务配置
applicationContext-tx.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<aop:config>
<aop:advisor advice-ref="txAdvicer" pointcut="execution(* com.hemi.service..*(..))"/>
</aop:config>
<tx:advice transaction-manager="transactionManager" id="txAdvicer">
<tx:attributes>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="delect*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
</beans>
springMVC
web.xml
<servlet>
<servlet-name>dispatherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
json数据发送
@RequestBody jackson的3个jar包
@RequestMapping("jsonToBean")
@ResponseBody
public User jsonToBean(@RequestBody User user){
return user;
}
contentType:appliation/json;charset=utf-8 JSON.stringify(json),
var json={username:"lisi",password:"123"};//定义json对象
$(function(){
$("#btn").click(function(){
$.ajax({
url:"/springmvc/jsonToBean",
type:"post",
contentType:"application/json;charset=utf-8",
//js原生的一个将json对象转成json字符串的工具类
data:JSON.stringify(json),
dataType:"text",
success:function(data){
$("#content").html(data);
}
});
});
});