发表于: 2018-03-14 20:56:26
1 572
今天完成的事情:
spring整合mybatis
项目结构
student类
package com;
public class Student {
private int ID;
private String create_at;
private String update_at;
private String name;
private String dailyLink;
private int QQ;
private String onlineNumber;
private String mail;
private int phone;
private String enrollmentTime;
private String professionType;
private String brotherName;
private String promise;
@Override
public String toString() {
return "student{" +
"id=" + ID +
", name='" + name + '\'' +
'}';
}
public Integer getId() {
return ID;
}
public void setId(Integer ID) {
this.ID = ID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCreate_at() {
return create_at;
}
public void setCreate_at(String create_at) {
this.create_at = create_at;
}
public String getUpdate_at() {
return update_at;
}
public void setUpdate_at(String update_at) {
this.update_at = update_at;
}
public String getDailyLink() {
return dailyLink;
}
public void setDailyLink(String dailyLink) {
this.dailyLink = dailyLink;
}
public String getOnlineNumber() {
return onlineNumber;
}
public void setOnlineNumber(String onlineNumber) {
this.onlineNumber = onlineNumber;
}
public String getEnrollmentTime() {
return enrollmentTime;
}
public void setEnrollmentTime(String enrollmentTime) {
this.enrollmentTime = enrollmentTime;
}
public String getProfessionType() {
return professionType;
}
public void setProfessionType(String professionType) {
this.professionType = professionType;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getBrotherName() {
return brotherName;
}
public void setBrotherName(String brotherName) {
this.brotherName = brotherName;
}
public String getPromise() {
return promise;
}
public void setPromise(String promise) {
this.promise = promise;
}
public int getQQ() {
return QQ;
}
public void setQQ(int QQ) {
this.QQ = QQ;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
}
mapper接口
package com;
/**
* @author: Arike
* @program: springMybatis2
* @description: spring, mybatis整合
* @create: 2018/3/14 下午3:30
*/
public interface StudentMapper {
//根据id查询用户信息
public Student findUserById(int id) throws Exception;
//根据用户名模糊查询
// public List<User> findUserByName(String name) throws Exception;
//添加用户信息
// public void insertUser(User user) throws Exception;
//删除用户信息
// public void deleteUser(int id) throws Exception;
//更新用户信息
// public void updateUser(User user) throws Exception;
}
spring配置
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--加载properties文件,获取数据库连接的一些信息-->
<context:annotation-config />
<!-- 加载classpath下的db.properties文件,里面配了数据库连接的一些信息 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="${dataSource}" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!--<property name="maxActive" value="10" /> -->
<!--<property name="maxIdle" value="5" />-->
</bean>
<!-- 配置sqlSessionFactory,SqlSessionFactoryBean是用来产生sqlSessionFactory的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载mybatis的全局配置文件,放在classpath下的mybatis文件夹中了 -->
<!--<property name="configLocation" value="sqlMapConfig.xml" />-->
<!-- 加载数据源,使用上面配置好的数据源 -->
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:StudentMapper.xml"/>
</bean>
<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.StudentMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
db.properties数据库配置
dataSource=org.apache.commons.dbcp.BasicDataSource
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydatabases
jdbc.username=root
jdbc.password=root
StudentMapper.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.StudentMapper">
<select id="findUserById" parameterType="int" resultType="com.Student">
select * from student where ID = #{id}
</select>
<!--<select id="findUserByname" parameterType="java.lang.String" resultType="com.Student">-->
<!--select * from student where username like '%${value}%'-->
<!--</select>-->
<!--<insert id="insertUser" parameterType="com.Student">-->
<!--insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})-->
<!--<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">-->
<!--select last_insert_id()-->
<!--</selectKey>-->
<!--</insert>-->
<!--<delete id="deleteUser" parameterType="java.lang.Integer">-->
<!--delete from student where id=#{id}-->
<!--</delete>-->
<!--<update id="updateUser" parameterType="com.Student">-->
<!--update user set name=#{username} where ID=#{id}-->
<!--</update>-->
</mapper>
SpringMybatisTest测试类
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import com.Student;
import com.StudentMapper;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringMybatisTest {
/* private ApplicationContext applicationContext;*/
@Autowired
StudentMapper studentMapper;
/*@Before
public void setup(){
applicationContext=new ClassPathXmlApplicationContext("beans.xml");
}*/
@Test
public void testFindUserById1() throws Exception {
// com.UserMapper userMapper = (com.UserMapper) applicationContext.getBean("userMapper");
Student student = studentMapper.findUserById(1);
System.out.println(student);
}
}
明天计划的事情:
在服务器上配置所需的东西,总结知识
遇到的问题:
这个时候需要在pom.xml中添加连接池
<!-- 添加阿里巴巴连接池 -->
<!--<dependency>-->
<!--<groupId>commons-dbcp</groupId>-->
<!--<artifactId>commons-dbcp</artifactId>-->
<!--<version>1.2.2</version>-->
<!--</dependency>-->
现在看就能连接数据源了
<!-- 配置数据源 -->
<bean id="dataSource" class="${dataSource}" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!--<property name="maxActive" value="10" /> -->
<!--<property name="maxIdle" value="5" />-->
</bean>
收获:
评论