0.准备工作
1.构建一个maven项目(无需模板)
2.在maven项目中的pom.xml中导入项目依赖和资源过滤设置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.cc</groupId> 8 <artifactId>ssmbuild</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 12 <!--依赖问题 Junit 数据库驱动 连接池 servlet jsp mybatis mybatis-spring spring --> 13 <dependencies> 14 <!--Junit--> 15 <dependency> 16 <groupId>junit</groupId> 17 <artifactId>junit</artifactId> 18 <version>4.12</version> 19 </dependency> 20 21 <!--数据库驱动--> 22 <dependency> 23 <groupId>mysql</groupId> 24 <artifactId>mysql-connector-java</artifactId> 25 <version>5.1.47</version> 26 </dependency> 27 28 <!--数据库连接池--> 29 <dependency> 30 <groupId>com.mchange</groupId> 31 <artifactId>c3p0</artifactId> 32 <version>0.9.5.2</version> 33 </dependency> 34 35 <!--Servlet Jsp--> 36 <dependency> 37 <groupId>javax.servlet</groupId> 38 <artifactId>servlet-api</artifactId> 39 <version>2.5</version> 40 </dependency> 41 <dependency> 42 <groupId>javax.servlet.jsp</groupId> 43 <artifactId>jsp-api</artifactId> 44 <version>2.2</version> 45 </dependency> 46 <dependency> 47 <groupId>javax.servlet</groupId> 48 <artifactId>jstl</artifactId> 49 <version>1.2</version> 50 </dependency> 51 52 <!--Mybatis--> 53 <dependency> 54 <groupId>org.mybatis</groupId> 55 <artifactId>mybatis</artifactId> 56 <version>3.5.2</version> 57 </dependency> 58 <dependency> 59 <groupId>org.mybatis</groupId> 60 <artifactId>mybatis-spring</artifactId> 61 <version>2.0.3</version> 62 </dependency> 63 64 <!--Spring--> 65 <dependency> 66 <groupId>org.springframework</groupId> 67 <artifactId>spring-webmvc</artifactId> 68 <version>5.2.12.RELEASE</version> 69 </dependency> 70 <dependency> 71 <groupId>org.springframework</groupId> 72 <artifactId>spring-jdbc</artifactId> 73 <version>5.2.12.RELEASE</version> 74 </dependency> 75 76 <!--lombok--> 77 <dependency> 78 <groupId>org.projectlombok</groupId> 79 <artifactId>lombok</artifactId> 80 <version>1.18.12</version> 81 </dependency> 82 83 </dependencies> 84 85 86 <!--静态资源导出问题--> 87 <build> 88 <resources> 89 <resource> 90 <directory>src/main/resources</directory> 91 <includes> 92 <include>**/*.properties</include> 93 <include>**/*.xml</include> 94 </includes> 95 <filtering>true</filtering> 96 </resource> 97 <resource> 98 <directory>src/main/java</directory> 99 <includes> 100 <include>**/*.properties</include> 101 <include>**/*.xml</include> 102 </includes> 103 <filtering>true</filtering> 104 </resource> 105 </resources> 106 </build> 107 108 109 </project>
3.在main.java下分别建立controller层,service层,dao层,pojo层
4.在MySQL数据库下建立一个新的database,并创建新的表
针对自己项目而定
1.整合Mybatis层
1.在resources目录下,建立mybatis核心配置文件,mybatis-config.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 7 <typeAliases> 8 <package name="com.cc.pojo"/> 9 </typeAliases> 10 11 <mappers> 12 <mapper class="注册对应的Mapper,这里填写路径"/> 13 </mappers> 14 15 16 </configuration>
此时也可以同时添加一个spring的配置文件applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 6 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 7 8 </beans>
再建立一个数据库连接信息的db.properties
1 jdbc.driver=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8 3 jdbc.username=root 4 jdbc.password=*************
2.在pojo层与数据库表中对应的实体类
3.在dao层创建操作实体类的接口
接口与pojo中的实体类对应,里面声明了需要的操作函数
以book为例BookMapper的接口和BookMapper.xml如下,
将接口中的操作需要的SQL在对应接口的xml文件中配置
每个接口函数绑定一个SQL
1 public interface BookMapper { 2 int addBook(Books books); 3 int deleteBookById(@Param("bookId") int id); 4 int updateBook(Books books); 5 Books queryBookById(@Param("bookId")int id); 6 List<Books> queryAllBook(); 7 }
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <mapper namespace="com.cc.dao.BookMapper"> 7 <insert id="addBook" parameterType="Books"> 8 insert into ssmbuild.books(bookName, bookCounts, detail) 9 values (#{bookName},#{bookCounts},#{detail}) 10 </insert> 11 12 <delete id="deleteBookById" parameterType="int"> 13 delete from ssmbuild.books 14 where bookID = #{bookId} 15 </delete> 16 17 <update id="updateBook" parameterType="Books"> 18 update ssmbuild.books 19 set bookName = #{bookName},bookCounts=#{bookCounts},detail=#{detail} 20 where bookID = #{bookID} 21 </update> 22 23 <select id="queryBookById" resultType="Books" parameterType="int"> 24 select * 25 from ssmbuild.books 26 where bookID = #{bookID} 27 </select> 28 29 <select id="queryAllBook" resultType="Books"> 30 select * from ssmbuild.books 31 </select> 32 </mapper>
4.将mapper注册到mybatis核心配置文件中