发表于: 2022-06-14 20:02:49
1 587
遇到问题:The error may exist in com/mapper/UserMapper.xml
发现是路径错误
<mappers>
<!-- 映射器告诉MyBatis到哪里去找映射文件-->
<mapper resource="com/mapper/UserMapper.xml"/>
</mappers>
右键UserMapper.xml文件选择copu path
将路径更改问题解决:
<mappers>
<!-- 映射器告诉MyBatis到哪里去找映射文件-->
<mapper resource="com.mapper/UserMapper.xml"/>
</mappers>
遇到问题:org.apache.ibatis.exceptions.PersistenceException:
解决方法缺少数据配置:
<!--配置环境-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/fc?useSSL=false&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="adbrk"/>
</dataSource>
</environment>
</environments>
遇到问题:Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.2
解决方法:在pom.xml文件中添加配置
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
问题解决
将ssm框架的代码意思弄清楚:
//实体类
public class Pc {
private int id;
private String name;
private int age;
private String type;
private String sex;
private String school;
service接口
//service 是应用层对外提供的接口,作用是实现用户示例
public interface PcService {
List<Pc> selectPcByName(Pc pc);
}
service实现类:
//标记这个类为spring容器的bean
@Component("PcService")
public class PcServiceImpl implements PcService {
//自动装配
@Autowired
private PcService pcService;
//重写selectPcByName方法
@Override
public List<Pc> selectPcByName(Pc pc) {
return pcService.selectPcByName(pc);
}
}
DAO接口
//使用Spring自动扫描MyBatis的接口并装配 (Spring将指定包中所有被@Mapper注解标注的接口自动装配为Mybatis的映射接口
@Repository("pcDao")
public interface PcDao {
//接口方法对应SQL映射文件UserMapper.xml中的id
List<Pc>selectPcByName(Pc pc);
}
controller控制器类
@Controller
public class PcController {
//自动配装
@Autowired
private PcDao pcDao;
//请求映射地址
@RequestMapping("/select")
public String Select(Pc pc , Model model){
List<Pc> list = pcDao.selectPcByName(pc);
//往前台传数据,可以传对象
model.addAttribute("pcList",list);
return "pcList";
}
}
返回的jsp数据
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%-- 表示描述文档类型 文档类型,这里为html,如果JS就是text/javascript--%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%-- JSP取得绝对路径的方法--%>
<form action="${pageContext.request.contextPath }/select" method="post">
输入用户名:<input type="text" name="name"/><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
propreties类
db.driver=com.mysql.jdbc.Driver
#数据库连接字符串(改成自己的连接)
db.url=jdbc:mysql://localhost:3306/fc?characterEncoding=utf-8&autoReconnect=true&allowMutiQueries=true&serverTimezone=Asia/Shanghai"
#数据库用户名(这里改成自己的用户名)
db.username=root
#数据库密码(这里改成自己的密码)
db.password=adbrk
mapper映射的数据
<?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.weixiao.dao.PcDao">
<!-- 接受用户输入数值-->
<select id="selectPcByName" resultType="com.weixiao.Pc"
parameterType="com.weixiao.Pc">
select * from pc where 1=1
<if test="name!=null and name!=''">
-- “%”代表0个或多个字符
and name like concat('%',#{name},'%')
</if>
</select>
</mapper>
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>
<mappers>
<!-- 映射器告诉MyBatis到哪里去找映射文件-->
<mapper resource="com/mapper/UserMapper.xml"/>
<mapper resource="com/mapper/PcMapper.xml"/>
</mappers>
</configuration>
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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--启用spring的一些annotation -->
<context:component-scan base-package="com.weixiao.Service"/>
<!--引入数据库配置文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--配置数据源-->
<!-- 这里用的是dbcp连接池,也可以换成druid、c3p0 -->
<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
<property name="maxActive" value="10"/>
<property name="maxIdle" value="5"/>
</bean>
<!--配置sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--指定Mybatis项目指定的配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--指定数据源-->
<property name="dataSource" ref="dataSource1"/>
</bean>
<!-- 配置mapper扫描,动态实现了mapper接口可以注册到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 要扫描的mapper -->
<property name="basePackage" value="com.weixiao.dao"/>
</bean>
<!--jdbc配置-->
<bean id="biageDao" class="com.weixiao.JdbcTemplateTest"/>
<bean id="biageService" class="com.weixiao.JdbcTemplateTest"/>
<!--系统默认配置的连接池-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="root"></property>
<property name="password" value="adbrk"></property>
<property name="url" value="jdbc:mysql://localhost:3306/fc"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
收获:以上
明天计划:完成接下来的任务
评论