2023最新IDEA版本Java零基础教程,赵姗姗老师带你7天入门Java架构师!
【【马士兵教育】2023最新IDEA版本Java零基础教程,赵姗姗老师带你7天入门Java架构师!】https://www.bilibili.com/video/BV1v24y1H74t?vd_source=d4c8a130bd8760e7e8cdd48d80abd00c
XML讲解
<?xml version="1.0" encoding="utf-8" ?>
<!--
version:版本号
encoding:编码格式
-->
<students>
<student id="1">
<name>ZhangSan</name>
<age>18</age>
<sex>男</sex>
<score>89.5</score>
</student>
<student id="2">
<name>LuLu</name>
<age>19</age>
<sex>女</sex>
<score>95.5</score>
</student>
<student id="3">
<name>MingMing</name>
<age>17</age>
<sex>女</sex>
<score>80</score>
</student>
</students>
package com.liweixiao.test17;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.util.Iterator;
import java.util.List;
/**
* @author:LiWeixiao
* @date:2023/7/12
* @description:
*/
public class Test {
public static void main(String[] args) throws DocumentException {
SAXReader sr = new SAXReader();
Document dom = sr.read(new File("E:\\hw\\IdeaProjects\\TestBiliPro\\TestFirstModule\\src\\students.xml"));
System.out.println(dom);
Element studentsEle = dom.getRootElement();
Iterator<Element> it1 = studentsEle.elementIterator();
while (it1.hasNext()){
Element studentEle = it1.next();
List<Attribute> atts = studentEle.attributes();
for(Attribute a:atts){
System.out.println("该子节点的属性:"+a.getName()+"---"+a.getText());
}
Iterator<Element> it2 = studentEle.elementIterator();
while (it2.hasNext()){
Element eles = it2.next();
System.out.println("节点:"+eles.getName()+"---"+eles.getText());
}
System.out.println();
}
}
}
JDBC
1.加载驱动
2.获取数据库连接Connection
3.创建会话-SQL命令发送器(Statement)
4.通过Statement发送SQL命令并得到结果
5.处理结果
6.关闭数据库资源(ResultSet、Statement、Connection)
createStatement不安全,prepareStatement安全。
executeUpdate增删改操作,executeQuery查询操作。
package com.liweixiao.test01;
import java.sql.*;
/**
* @author:LiWeixiao
* @date:2023/7/12
* @description:
*/
public class Test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=utf8&useSSL=true";
String userName="root";
String pwd="123456";
Connection conn = DriverManager.getConnection(url, userName, pwd);
Statement sta = conn.createStatement();
//int i = sta.executeUpdate("INSERT INTO t_book(id,NAME,author,price) VALUES (3,'C++','王五',50)");
int i = sta.executeUpdate("delete from t_book where id=3");
System.out.println(i);
if(i>0){
System.out.println("更新成功");
}else {
System.out.println("更新失败");
}
ResultSet rs = sta.executeQuery("select * from t_book where price < 90");
while (rs.next()){
System.out.println(rs.getInt("id"));
System.out.println(rs.getString("name"));
System.out.println(rs.getString("author"));
System.out.println(rs.getDouble("price"));
System.out.println();
}
sta.close();
conn.close();
}
}
MyBatis
1.创建数据库表
2.创建Maven项目
3.添加依赖
4.创建MyBatis全局配置文件
5.创建实体类
6.创建映射文件
7.编写测试类,启动项目
sqlSession,查询不用commit,增删改需要commit
【一、maven的pom】
pom.xml
<?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.liweixiao</groupId>
<artifactId>TestMyBatis02</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--mysql驱动的依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--mybatis驱动的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<!--log4j驱动的依赖-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
【二、resources】
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=utf-8&useSSL=true
username=root
password=123456
log4j.properties
# Log4j中定义的级别
log4j.rootLogger=error, stdout
# 命名空间
log4j.logger.com.liweixiao.mapper.BookMapper=error
# console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
mybatis.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>
<properties resource="db.properties"></properties>
<typeAliases>
<package name="com.liweixiao.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/liweixiao/mapper/BookMapper.xml"/>
</mappers>
</configuration>
BookMapper.xml
com/liweixiao/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.liweixiao.mapper.BookMapper">
<select id="selectAllBooks" resultType="Book">
select * from t_book
</select>
<select id="selectOneBook" resultType="Book">
select * from t_book where name = #{arg0} and author = #{arg1}
</select>
<select id="selectOneBook2" resultType="Book">
select * from t_book where name = #{name} and author = #{author}
</select>
<select id="selectOneBook3" resultType="Book">
select * from t_book where name = #{arg0} and author = #{arg1.author}
</select>
<insert id="addOneBook">
insert into t_book(id,name,author,price) values (#{id},#{name},#{author},#{price})
</insert>
<delete id="deleteOneBook">
delete from t_book where id = #{id}
</delete>
<update id="updateOneBook">
update t_book set price=price/2 where id = #{id}
</update>
</mapper>
【三、java】
Book.java
package com.liweixiao.pojo;
/**
* @author:LiWeixiao
* @date:2023/7/13
* @description:搭建第一个MyBatis框架
*/
public class Book {
private int id;
private String name;
private String author;
private double price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Book() {
}
public Book(int id, String name, String author, double price) {
this.id = id;
this.name = name;
this.author = author;
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
'}';
}
}
BookMapper.java
package com.liweixiao.mapper;
import com.liweixiao.pojo.Book;
import java.util.List;
/**
* @author:LiWeixiao
* @date:2023/7/13
* @description:
*/
public interface BookMapper {
//查询
/*public abstract */List selectAllBooks();
public abstract Book selectOneBook(String name, String author);
public abstract Book selectOneBook2(Book book);
public abstract Book selectOneBook3(String name,Book book);
//新增
public abstract int addOneBook(Book book);
//删除
public abstract int deleteOneBook(int id);
//修改
public abstract int updateOneBook(int id);
}
【四、test】
Test.java
package com.liweixiao.test;
import com.liweixiao.mapper.BookMapper;
import com.liweixiao.pojo.Book;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* @author:LiWeixiao
* @date:2023/7/13
* @description:
*/
public class Test {
public static void main(String[] args) throws IOException {
//指定核心配置文件的路径
String resource = "mybatis.xml";
//获取加载配置文件的输入流
InputStream inputStream = Resources.getResourceAsStream(resource);
//加载配置文件
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//通过工厂类获取一个会话
SqlSession sqlSession = sqlSessionFactory.openSession();
//动态代理模式?BookMapper mapper = BookMapper实现类,多态的一种方式
BookMapper mapper = sqlSession.getMapper(BookMapper.class);
List list = mapper.selectAllBooks();
for (int i = 0; i < list.size(); i++) {
Book b = (Book) (list.get(i));
System.out.println(b);
}
sqlSession.close();
}
}
/*Book book = mapper.selectOneBook("Python", "李四");*/
Book b2 = new Book();
b2.setName("Python");
b2.setAuthor("李四");
/*Book book = mapper.selectOneBook2(b2);*/
Book book= mapper.selectOneBook3("Python", b2);
System.out.println(book);
Book b = new Book(4, "go", "赵六", 18.8);
int i = mapper.addOneBook(b);
if(i>0){
System.out.println("新增一条");
}else {
System.out.println("新增失败");
}
//事务相关的操作
sqlSession.commit();
int i = mapper.deleteOneBook(4);
if(i>0){
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
sqlSession.commit();
int i = mapper.updateOneBook(2);
if(i>0){
System.out.println("更新成功");
}else {
System.out.println("更新失败");
}
sqlSession.commit();
Spring
IOC控制反转,也被称为DI依赖注入。
【一、maven的pom】
pom.xml
<?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.liweixiao</groupId>
<artifactId>TestSpring01</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.23</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
【二、resources】
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--属性的设值注入-->
<bean id="b" class="com.liweixiao.pojo.Book">
<property name="id" value="1"></property>
<property name="name" value="Java"></property>
</bean>
<!--属性的构造注入-->
<bean id="b2" class="com.liweixiao.pojo.Book">
<constructor-arg name="id" value="2"></constructor-arg>
<constructor-arg name="name" value="Python"></constructor-arg>
</bean>
<!--<bean id="boy" class="com.liweixiao.pojo.Boy">
<property name="age" value="20"></property>
<property name="name" value="小强"></property>
</bean>
<bean id="girl" class="com.liweixiao.pojo.Girl">
<property name="age" value="19"></property>
<property name="name" value="露露"></property>
<property name="boyfriend" ref="boy"></property>
</bean>
<bean id="boy2" class="com.liweixiao.pojo.Boy">
<constructor-arg name="age" value="23"></constructor-arg>
<constructor-arg name="name" value="小明"></constructor-arg>
</bean>
<bean id="girl2" class="com.liweixiao.pojo.Girl">
<constructor-arg name="age" value="21"></constructor-arg>
<constructor-arg name="name" value="喵喵"></constructor-arg>
<constructor-arg name="boyfriend" ref="boy2"></constructor-arg>
</bean>-->
</beans>
【三、java】
Book.java
package com.liweixiao.pojo;
/**
* @author:LiWeixiao
* @date:2023/7/14
* @description:
*/
public class Book {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
System.out.println("id的setter方法");
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("name的setter方法");
this.name = name;
}
public Book() {
System.out.println("Book类的空构造器");
}
public Book(int id, String name) {
System.out.println("Book类的有参构造器");
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
【四、test】
Test.java
package com.liweixiao.test;
import com.liweixiao.pojo.Book;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author:LiWeixiao
* @date:2023/7/14
* @description:
*/
public class Test {
public static void main(String[] args) {
//创建Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取对象
Book b = (Book) (context.getBean("b"));
System.out.println(b);
Book b2 = (Book) (context.getBean("b2"));
System.out.println(b2);
//获取对象
/*Girl g = (Girl) (context.getBean("girl"));*/
/*Girl g = (Girl) (context.getBean("girl2"));
System.out.println(g);*/
Girl g = (Girl) (context.getBean("girl"));
System.out.println(g);
Boy b = (Boy) (context.getBean("boy"));
System.out.println(b);
}
}
【补充:注解的使用】
applicationContext.xml中添加
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.liweixiao.pojo"></context:component-scan>
</beans>
Girl.java
package com.liweixiao.pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Girl {
@Value("18")
private int age;
@Value("莎莎")
private String name;
@Autowired
private Boy boyfriend;
Boy.java
package com.liweixiao.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Boy {
@Value("22")
private int age;
@Value("小壮")
private String name;
}
SpringMVC
【一、maven的pom】
新建webapp项目
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.liweixiao</groupId>
<artifactId>TestSpringMVC</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>TestSpringMVC Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.23</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/TestSpringMVC</path>
<port>8080</port>
</configuration>
</plugin>
</plugins>
<finalName>TestSpringMVC</finalName>
</build>
</project>
【二、resources】
springmvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描包-->
<context:component-scan base-package="com.liweixiao.controller"></context:component-scan>
<!--让注解@RequestMapping生效-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
【三、java】
TestController.java
package com.liweixiao.controller;
import com.liweixiao.pojo.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author:LiWeixiao
* @date:2023/7/17
* @description:控制器类
*/
@Controller
public class TestController {
@RequestMapping("/test1")
public String test1(){
return "index.jsp";
}
@RequestMapping("/testParam")
public String testParam(String name,int age){
System.out.println(name+"---"+age);
return "index1.jsp";
}
@RequestMapping("/testParam2")
public String testParam2(Person person){
System.out.println(person);
return "index1.jsp";
}
}
Person.java,包含name、age等属性
【四、webapp】
web.xml,程序的入口
<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>springmvc</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>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
案例SSM书城
【一、maven的pom】
新建webapp项目
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.liweixiao</groupId>
<artifactId>TestSSM</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--1、mybatis驱动的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.9</version>
</dependency>
<!--2、mysql驱动的依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--3、log4j驱动的依赖-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--4、spring核心依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.23</version>
</dependency>
<!--5、springjdbc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.24</version>
</dependency>
<!--6、spring整合mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!--7、springMVC依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.23</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--加入tomcat7插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/testssm</path>
<port>8080</port>
</configuration>
</plugin>
</plugins>
</build>
</project>
【二、resources】
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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--1、连接数据库,获取数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--属性的设值注入-->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=utf-8&useSSL=true"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--2、获取SqlSessionFactory对象-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
<!--给包下类起别名-->
<property name="typeAliasesPackage" value="com.liweixiao.pojo"/>
<!--解析mybatis.xml-->
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
<!--3、扫描mapper文件-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="factory"></property>
<property name="basePackage" value="com.liweixiao.mapper"></property>
</bean>
<!--4、扫描包下注解-->
<context:component-scan base-package="com.liweixiao.serviceImpl"></context:component-scan>
</beans>
springmvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描包-->
<context:component-scan base-package="com.liweixiao.controller"></context:component-scan>
<!--让注解@RequestMapping生效-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
log4j.properties见上文
mybatis.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="logImpl" value="LOG4J"/>
</settings>
</configuration>
BookMapper.xml
com/liweixiao/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.liweixiao.mapper.BookMapper">
<select id="selectAll" resultType="book">
select * from t_book
</select>
</mapper>
【三、java】
com.liweixiao.pojo.Book.java见上文
com.liweixiao.mapper.BookMapper.java
package com.liweixiao.mapper;
import java.util.List;
/**
* @author:LiWeixiao
* @date:2023/7/18
* @description:
*/
public interface BookMapper {
List selectAll();
}
com.liweixiao.service.BookService.java
package com.liweixiao.service;
import java.util.List;
/**
* @author:LiWeixiao
* @date:2023/7/18
* @description:
*/
public interface BookService {
List findAll();
}
com.liweixiao.serviceImpl.BookServiceImpl.java
package com.liweixiao.serviceImpl;
import com.liweixiao.mapper.BookMapper;
import com.liweixiao.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author:LiWeixiao
* @date:2023/7/18
* @description:
*/
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookMapper bookMapper;
public List findAll() {
return bookMapper.selectAll();
}
}
com.liweixiao.controller.BookController.java
package com.liweixiao.controller;
import com.liweixiao.pojo.Book;
import com.liweixiao.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
* @author:LiWeixiao
* @date:2023/7/18
* @description:
*/
@Controller
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping(value = "/findAllBooks",produces = "text/html;charset=utf-8")
/*tomcat8以上支持ResponseBody*/
@ResponseBody
public String findAll(){
List list = bookService.findAll();
System.out.println("一共有几本书籍:"+list.size());
String books="";
for (int i = 0; i < list.size(); i++) {
Book book = (Book) (list.get(i));
books += book.getId();
books += book.getName();
books += book.getPrice();
}
return books;
}
}
【四、webapp】
web.xml,程序的入口
<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>springmvc</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>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--监听器监听-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
SpringBoot案例书城
【1、maven的pom】
pom.xml
<?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.liweixiao</groupId>
<artifactId>TestSpringBoot</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.6</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
</project>
【2、resource】
application.yml
server:
port: 8888
servlet:
context-path: /springbootssm
spring:
datasource:
url: jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=utf-8&useSSL=true
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
mybatis:
type-aliases-package: com.liweixiao.pojo
mapper-location: classpath:com/liweixiao/mapper/*.xml
BookMapper.xml
com/liweixiao/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.liweixiao.mapper.BookMapper">
<select id="selectAllBooks" resultType="book">
select * from t_book
</select>
</mapper>
【3、java】
com.liweixiao.pojo.Book.java见上文
com.liweixiao.mapper.BookMapper.java
package com.liweixiao.mapper;
import java.util.List;
/**
* @author:LiWeixiao
* @date:2023/7/19
* @description:
*/
public interface BookMapper {
List selectAllBooks();
}
com.liweixiao.service.BookService.java
package com.liweixiao.service;
import java.util.List;
/**
* @author:LiWeixiao
* @date:2023/7/19
* @description:
*/
public interface BookService {
List findAllBooks();
}
com.liweixiao.service.impl.BookServiceImpl.java
package com.liweixiao.service.impl;
import com.liweixiao.mapper.BookMapper;
import com.liweixiao.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author:LiWeixiao
* @date:2023/7/19
* @description:
*/
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookMapper bookMapper;
public List findAllBooks() {
return bookMapper.selectAllBooks();
}
}
com.liweixiao.controller.BookController.java
package com.liweixiao.controller;
import com.liweixiao.pojo.Book;
import com.liweixiao.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
* @author:LiWeixiao
* @date:2023/7/19
* @description:
*/
@Controller
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping("/findBooks")
// @RequestMapping(value = "/findBooks",produces = "text/html;charset=utf-8")
@ResponseBody
public String findBooks(){
List list = bookService.findAllBooks();
String s="";
for (int i = 0; i < list.size(); i++) {
Book book = (Book) (list.get(i));
s += book.getName();
s += book.getPrice();
}
return s;
}
}
【4、程序入口】
com.liweixiao.TestSpringBootApplication.java
package com.liweixiao;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author:LiWeixiao
* @date:2023/7/19
* @description:
*/
@SpringBootApplication
@MapperScan("com.liweixiao.mapper")
public class TestSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(TestSpringBootApplication.class,args);
}
}