赞
踩
在最开始我们使用jdbcUtil的方式进行硬编码,sql字符串写的很难受,使用mybatis可以解决这个问题,它提供了数据库与实体类的关系映射,通过在xml我们可灵活编写sql语句,同时mybatis提供了流程控制的动态标签,可以帮助我们更好的编写sql
<!-- Mybatis核心 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <!-- MySQL驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.16</version> </dependency> <!-- mybatis逆向工程依赖 --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.16</version> </dependency>
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.url:jdbc:mysql://localhost:3306/testdb?characterEncoding=UTF-8&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&tinyInt1isBit=false&zeroDateTimeBehavior=convertToNull
jdbc.username=root
jdbc.password=root
<?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> <properties resource="jdbc.properties"></properties> <settings> <!-- 映射下划线到驼峰命名 last_name ==> lastName --> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- 开启延迟加载 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 指定加载的属性是按需加载 --> <!-- <setting name="aggressiveLazyLoading" value="false"/>--> <!-- 二级缓存 --> <!-- <setting name="cacheEnabled" value="true"/>--> <!-- log4j日志开启--> <!-- <setting name="logImpl" value="LOG4J"/>--> </settings> <!--起别名--> <typeAliases> <package name="com.testMaven.entity"/> <package name="com.testMaven.vo"/> <package name="com.testMaven.dto"/> </typeAliases> <!-- 分页插件,可能会因为版本过高会报错--> <!-- <plugins>--> <!-- <plugin interceptor="com.github.pagehelper.PageHelper">--> <!-- <property name="dialect" value="mysql"/>--> <!-- </plugin>--> <!-- </plugins>--> <!-- 数据环境可以配置多个--> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driverClass}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> <!-- <environment id="dev_oracle">--> <!-- <transactionManager type="JDBC" />--> <!-- <dataSource type="POOLED">--> <!-- <property name="driver" value="${orcl.driver}" />--> <!-- <property name="url" value="${orcl.url}" />--> <!-- <property name="username" value="${orcl.username}" />--> <!-- <property name="password" value="${orcl.password}" />--> <!-- </dataSource>--> <!-- </environment>--> </environments> <mappers> <!-- <package name="com.testMaven.mapper"/>--> <mapper resource="mapper/CommentIfnoMapper.xml"/> <mapper resource="mapper/AnnounceInfoMapper.xml"/> </mappers> </configuration>
可以帮助我们生成mapper、xml、实体类文件,只要引入相关依赖,在运行相关的类就行了,还有一种方式运行逆向工程,在pom.xml配置一个插件,在maven中启动插件也能生成代码,不过后面我们mybatis-plus我们基本都是用mybatisx、easycode生成代码
public class GeneratorTest {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("src/main/resources/mybatis-generator-config.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);
System.out.println("生成成功!");
}
}
在编写sql是如果你的数据库字段的日期是0000-00-00 00:00:00,映射过来可能会出错,需要在数据库后添加&zeroDateTimeBehavior=convertToNull
public class SqlSessionTest { private static SqlSessionFactory sqlSessionFactory; static { //静态代码块会随着类的加载而自动执行,且只执行一次 try { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException, ParseException { getCommentInfoByOr(); } public static void insert() throws ParseException { SqlSession sqlSession = sqlSessionFactory.openSession(true); CommentIfnoMapper commentIfnoMapper = sqlSession.getMapper(CommentIfnoMapper.class); Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String s = sdf.format(d); Date date = sdf.parse(s); CommentInfoDTO commentInfoDTO = new CommentInfoDTO("测试",1,1, date); int row = commentIfnoMapper.insertComment(commentInfoDTO); System.out.println(row); } }
欢迎指正解答
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。