<dependencies> <!-- mybatis所用的--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.10</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!-- spring所需--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.21</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.19</version> </dependency> <!-- mvc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.21</version> </dependency> <!-- spring整合Mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> </dependency> <!-- servlet--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies>
资源过滤:
<!--资源过滤--> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
建立基本结构和配置框架!
cn.laoyao.pojo
cn.laoyao.dao
cn.laoyao.service
cn.laoyao.controller
cn.laoyao.utils
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> </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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
1、数据库配置文件 database.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/数据库名?useSSL=true&useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=root
2、IDEA关联数据库
3、编写MyBatis的核心配置文件mybatis-config.xml及spring-mapper.xml
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="cn.laoyao.pojo"/> </typeAliases> <!--配置映射器--> <mappers> <mapper resource="cn/laoyao/dao/BookMapper"/> </mappers> </configuration>
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: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 https://www.springframework.org/schema/context/spring-context.xsd"> <!--关联配置文件,可以在spring中使用${}--> <context:property-placeholder location="classpath:db.properties"/> <!-- 2、连接池 dbcp:半自动化操作,不能自动连接 c3pe:自动化操作(自动化的加载配置文件,并且可以自动设置到对象中! ) druid : hikari --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--3、sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--绑定Mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!-- 4、配置dao接口扫描包,动态实现dao接口可以注入到spring容器中 也就是说不用写xxxMapperImpl类了 实现:【接口】->【spring容器】 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--注入sqlSessionFactory--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!--要扫描的包--> <property name="basePackage" value="cn.laoyao.dao"/> </bean> </beans>
4、编写数据库对应的实体类
@Data public class User { private String name; private String sex; private String age; }
5、编写Dao层的 UserMapper接口!
public interface UserMapper { /** * 根据id查询user */ User selectUserById(String id); }
6、编写接口对应的 UserMapper.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="cn.laoyao.dao.UserMapper"> <select id="selectUserById" resultType="cn.laoyao.pojo.User"> select * from test.user where id=#{id} </select> </mapper>
service接口
public interface UserService { /** * 根据id查询user */ User selectUserById(String id); }
实现类
@Service public class UserServiceImpl implements UserService { @Autowired UserMapper userMapper; @Override public User selectUserById(String id) { User user = userMapper.selectUserById(id); return user; } }
配置文件
spring-service.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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--1、扫描service下的包,使注解生效--> <context:component-scan base-package="cn.laoyao.service"/> <!--可以用下面方式注入,也可以使用@Service注解--> <!--2、将我们的所有业务类,注入到Spring,可以通过配置,或者注解--> <bean id="UserServiceImpl" class="cn.laoyao.service.UserServiceImpl"> <property name="userMapper" ref="userMapper"/> </bean> <!--3、声明式事务配置--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--注入数据源--> <property name="dataSource" ref="dataSource"/> </bean> <!--4、aop--> </beans>
spring-mvc.xml:
<!--开启mvc注解支持--> <mvc:annotation-driven/> <!--开启包扫描--> <context:component-scan base-package="cn.laoyao.controller"/>
@RestController public class MyController { @Autowired UserService userService; @RequestMapping("/query/{name}") public Object test(@PathVariable String name){ User user = userService.selectUserByName(name); return user; } }