标签:root import public bsp sql char hot inter end
一、maven
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wt</groupId> <artifactId>ssmbuild</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!--Junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> <!--数据库驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <!--数据库连接池--> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!--Servlet - JSP --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--Mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.5</version> </dependency> <!--Spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.0.RELEASE</version> </dependency> <!--JSON--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.11.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.60</version> </dependency> </dependencies> <!--静态资源到处问题--> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
二、整合mybatis
1、resource资源文件夹下,创建 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> <!--别名--> <typeAliases> <package name="com.wt.pojo"/> </typeAliases> <mappers> <mapper class="com.wt.mapper.BookMapper"/> </mappers> </configuration>
2、resource资源文件夹下, 创建spring的配置文件 applicationContext.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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> </beans>
3、创建数据库连接信息 db.properties (注意:数据库通过 spring连接)
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssmbuild?characterEncoding=UTF8
name=root
password=@WSX3edc
4、创建
com.wt.pojo
com.wt.mapper
com.wt.server
com.wt.controller
5、pojo
创建Books.java
package com.wt.pojo; public class Books { private int bookID; private String bookName; private int bookCounts; private String detail; public Books() { } public Books(int bookID, String bookName, int bookCounts, String detail) { this.bookID = bookID; this.bookName = bookName; this.bookCounts = bookCounts; this.detail = detail; } public int getBookID() { return bookID; } public void setBookID(int bookID) { this.bookID = bookID; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public int getBookCounts() { return bookCounts; } public void setBookCounts(int bookCounts) { this.bookCounts = bookCounts; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail; } @Override public String toString() { return "Books{" + "bookID=" + bookID + ", bookName=‘" + bookName + ‘\‘‘ + ", bookCounts=" + bookCounts + ", detail=‘" + detail + ‘\‘‘ + ‘}‘; } }
6、mapper层
BookMapper接口
package com.wt.mapper; import com.wt.pojo.Books; import org.apache.ibatis.annotations.Param; import java.awt.print.Book; import java.util.List; public interface BookMapper { // 增 int addBook(Books books); // 删 int deleteBook(@Param("id") int id); // 改 int updateBook(@Param("id") int id); // 查 int selectBook(@Param("id") int id); // 查所有 List<Book> getBooksList(); }
添加 mybatis配置文件的 别名 和 mapper
BooKMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.wt.mapper.BookMapper"> <insert id="addBook" parameterType="books"> insert into books(bookName, bookCounts, detail) values ( #{bookName}, #{bookCounts} #{detail} ) </insert> <delete id="deleteBook" parameterType="int"> delete from books where bookID = #{id} </delete> <update id="updateBook" parameterType="int"> update books set bookName = #{bookName}, bookCounts = #{bookCounts}, detail = #{detail} where bookID = #{id} </update> <select id="selectBook" parameterType="int" resultType="books"> select * from books where bookID = #{id} </select> <select id="getBooksList" resultType="books"> select * from books </select> </mapper>
7、service(层)
BookService 接口类
package com.wt.service; import com.wt.pojo.Books; import org.apache.ibatis.annotations.Param; import java.awt.print.Book; import java.util.List; public interface BookService { // 增 int addBook(Books books); // 删 int deleteBook(int id); // 改 int updateBook(int id); // 查 int selectBook(int id); // 查所有 List<Book> getBooksList(); }
BookServiceImpl.java
package com.wt.service; import com.wt.mapper.BookMapper; import com.wt.pojo.Books; import java.awt.print.Book; import java.util.List; public class BookServiceImpl implements BookService { // service(业务层) -> mapper层 private BookMapper bookMapper; public void setBookMapper(BookMapper bookMapper) { this.bookMapper = bookMapper; } public int addBook(Books books) { return bookMapper.addBook(books); } public int deleteBook(int id) { return bookMapper.deleteBook(id); } public int updateBook(int id) { return bookMapper.updateBook(id); } public int selectBook(int id) { return bookMapper.selectBook(id); } public List<Book> getBooksList() { return bookMapper.getBooksList(); } }
三、整合spring
1、创建spring-mapper.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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userService" class="com.wt.service.UserServiceImpl"/> <bean id="testLog" class="com.wt.diy.TestLog"/> <aop:aspectj-autoproxy /> </beans>
标签:root import public bsp sql char hot inter end
原文地址:https://www.cnblogs.com/wt7018/p/13344145.html