今天讲的是Mybatis入门
我们还需要更改一下web
<!--第一步就是配置maven-compiler-plugin插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 修改web.xml由2.3至3.0 -->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
</web-app>
<!--修改.settings中的org.eclipse.wst.common.project.facet.core.xml-->
<faceted-project>
<fixed facet="wst.jsdt.web"/>
<installed facet="java" version="1.7"/>
<installed facet="jst.web" version="3.0"/>
<installed facet="wst.jsdt.web" version="1.0"/>
</faceted-project>
mybits环境配置
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
添加Mybatis插件“mybatis plugins”
以离线方式安装“mybatis plugins”
Free mybatis plugin
Mybatis generater
mybatis tools
maven helper
逆向工程Mybatis-generator
5.1 MAVEN中使用mybatis-generator-maven-plugin插件
因为IntelliJ中没有mybatis-generato对应的插件,所以需要在MAVEN中使用 mybatis-generator-maven-plugin插件来完成功能
添加mybatis-generator-maven-plugin插件相关依赖:mybatis-generator-maven-plugin.pom.txt
5.2 配置generatorConfig.xml
resources下建generatorConfig.xml,作为mybatis-generator-maven-plugin插件的执行目标
5.3 在Intellij IDEA添加一个“Run运行”选项,使用maven运行mybatis-generator-maven-plugin插件
命令:mybatis-generator:generate -e
注1:XxxMapper.xml中的ID要与XxxMapper.java接口中的方法一致
注2:XxxMapper.java接口的方法,均只能定义一个参数
注3:使用Mybatis-generator时出现以下错误:“ Column name pattern can not be NULL or empty”
错误产生的原因是因为使用了高版本的mysql驱动,请使用较低版本的mysql驱动
注4:IDEA配置使用Mybatis出现 “Could not find resource”
问题:在编译后的target文件夹下,发现只有mapper的class文件,而没有xml文件,
将对应的xml文件放到这个文件夹下运行就不会出现下面的错误。说明出现这个错误的原因是maven编译时没有将xml文件放进去。
解决方法:在pom.xml中添加如下代码
src/main/java
**/.xml
注5:resource jdbc.properties does not exist
问题:在编译后的target文件夹下,而没有jdbc.properties文件,但同时不能少了.xml
src/main/resources
jdbc.properties
*.xml
mybatis之ssh与ssm的区别、案例演示
public class BookServiceImplTest {
private BookService bookService;
private SqlSession sqlSession;
@Before
public void setUp() throws Exception {
BookServiceImpl bookServiceImpl = new BookServiceImpl();
sqlSession = SessionUtil.openSession();
bookServiceImpl.setBookMapper(sqlSession.getMapper(BookMapper.class));
this.bookService = bookServiceImpl;
}
@After
public void tearDown() throws Exception {
sqlSession.commit();
sqlSession.close();
}
@Test
public void insert() {
Book book = new Book();
book.setBname("zzz");
book.setPrice(99f);
book.setBid(110003);
int insert = bookService.insert(book);
}
@Test
public void selectByPrimaryKey() {
Book book = this.bookService.selectByPrimaryKey(66);
System.out.println(book);
}
}