以下是MyBatis的约束及配置
<?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.bjpowernode.beans"/><!-- 写包名 -->
</typeAliases>
<!-- 映射文件 -->
<mappers>
<!-- <mapper resource = "com.bjpowernode.dao.mapper.xml"/> 其中一种方式 -->
<package name = "com.bjpowernode.dao"/> <!-- 写包名 不要把.class带着 很容易忽略掉 省心哈哈 -->
</mappers>
<!--以上别名配置和映射文件的配置属于一劳永逸的配置 记住 -->
</configuration>
以下是mapper.xml的约束及配置 这里mapper.xml更换包名 一劳永逸哈哈 与IStudentDao放在一个包中
<?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">
<!--约束文件 -->
<!-- xml约束配置 全名 -->
<mapper namespace="com.bjpowernode.dao.IStudentDao"><!-- 映射文件全路径 -->
<!-- 增删改查 你的业务时什么就写什么样的sql语句 -->
<insert id="insertStudent">
insert into student(name,age)values(#{name},#{age})
</insert>
<delete id="deleteById">
delete from student where id = #{xxx}
</delete>
<update id="updateStudent">
update student set name = #{name},age=#{age}where id = #{id}
</update>
<select id="selectAllStudents" resultType="Student">
select id,name,age from student
</select>
<select id="selectStudentById" resultType="Student">
select id,name,age from student where id = #{xxx}
</select>
</mapper>
以下是application.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内置连接池jdbc-->
<bean id="myDataSource" 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>
<!-- 注册属性文件 常用 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 生成mySqlessionFactory -->
<bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis.xml" />
<property name="dataSource" ref="myDataSource" />
</bean>
<!-- 生成dao对象 -->
<bean id="studentDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="mySqlSessionFactory" />
<property name = "mapperInterface" value = "com.bjpowernode.dao.IStudentDao"/>
</bean>
<!-- 注册service -->
<bean id="studentService"
class="com.bjpowernode.services.StudentServiceImp">
<property name="dao" ref="studentDao" />
</bean>
</beans>
以下方式二: 采用MapperScanerConfigurer 进行设置 她没有id属性
?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内置连接池jdbc-->
<bean id="myDataSource" 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>
<!-- 注册属性文件 常用 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 生成mySqlessionFactory -->
<bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis.xml" />
<property name="dataSource" ref="myDataSource" />
</bean>
<!-- 生成dao对象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 动态扫描设置 去掉id-->
<property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory" />
<property name = "basePackage" value = "com.bjpowernode.dao"/><!-- 基础包,对此包下的所有接口生成代理对象 -->
</bean>
<!-- 注册service -->
<bean id="studentService"
class="com.bjpowernode.services.StudentServiceImp">
<!-- 这里dao的注入需要使用ref属性,若接口名的前两个字母是大写,值为接口的简单类名
若dao的接口名字首字母大写后面的是小写,则值为接口的简单类名,但ref属性的值首字母要小写 -->
<property name="dao" ref="IStudentDao" /> 可能为studentDao要看接口名的设置
</bean>
</beans>
以下是jdbc.properties的简单设置
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test 若果地址为127.0.0.1:3306 这个地址直接可以省略写为jdbc:mysql:///test
jdbc.username=root 自己一定要记清楚用户名与密码否则找回可能会有点麻烦,但也不是不能找回。
jdbc.password=root
以上就是简单的Spring与Mybatis的设置,肯定还会有更简单的设置吧,一起进步吧。