package com.bwf.main;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages={"com.bwf.controller","com.bwf.service"})
@MapperScan(basePackages={"com.bwf.dao"})
public class StartSSM {
public static void main(String[] args) {
SpringApplication.run(StartSSM.class);
}
}
PersonController.java
package com.bwf.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bwf.entity.Person;
import com.bwf.service.IPersonService;
@Controller
public class PersonController {
@Autowired
private IPersonService personService;
@RequestMapping("m1")
//测试一下
public String m1(){
return "index";
}
@RequestMapping("m2")
@Transactional //以前配事务很麻烦,现在只用加此注解,一般在业务层加事务
//查询不用加事务,增删改查两条数据以上要加事务
//向数据库连续插入两条数据,事务操作
public String m2(){
System.out.println(personService);
personService.reg("张梦", 23);
//System.out.println(1/0);//用此异常测试事务
personService.reg("党银银", 27);
return "index";
}
@RequestMapping("m3")
public String m3(ModelMap mm){
//从数据库查询得到person表的一条数据
Person p = personService.find(9);
System.out.println(p.getName());
return "index";
}
@RequestMapping("m4")
@Transactional
//向数据库批量插入
public String m4(){
Person p1 = new Person();
p1.setName("王大锤");
Person p2 = new Person();
p2.setName("马可");
Person p3 = new Person();
p3.setName("古筝");
List list = new ArrayList();
list.add(p1);
list.add(p2);
list.add(p3);
personService.add(list);
return "index";
}
@RequestMapping("m5")
public String m5(ModelMap mm){
//从数据库取出整个person表,并将它显示在前台页面上
//System.out.println(personService.selectAll().size());
List list = personService.selectAll();
mm.addAttribute("pList", list);
//System.out.println(list);
return "person_db";
}
@RequestMapping("m6")
public String m6(){
//修改数据库中的某条数据
Person p = new Person();
p.setId(1);
p.setName("张君宝");
p.setAge(149);
personService.modify(p);
return "person_db";
}
}
IPersonService和PersonService
package com.bwf.service;
import java.util.List;
import com.bwf.entity.Person;
public interface IPersonService {
public void reg(String name,int age);
public Person find(int id);
public void add(List list);
public List selectAll();
public void modify(Person p);
}
package com.bwf.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bwf.dao.PersonMapper;
import com.bwf.entity.Person;
@Service
public class PersonService implements IPersonService {
@Autowired
private PersonMapper personMapper;
public void reg(String name, int age) {
System.out.println(personMapper);
//System.out.println("注册过了"+name+","+age);
personMapper.insertPerson(name, age);
}
public Person find(int id) {
return personMapper.selectPerson(id);
}
public void add(List list) {
for(int i = 0; i < list.size();i++){
personMapper.addPerson(list.get(i));
}
}
public List selectAll() {
return personMapper.selectAll();
}
public void modify(Person p) {
personMapper.setPerson(p);
}
}
PersonMapper.java
package com.bwf.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.bwf.entity.Person;
public interface PersonMapper {
public void insertPerson(@Param("name")String name,@Param("age")int age);
public Person selectPerson(int id);
public void addPerson(Person p);
public List selectAll();
public void setPerson(Person p);
}
Person.xml
INSERT INTO person(name,age)VALUES(#{name},#{age})
INSERT INTO person(id,name,age,sex,pwd)VALUES(#{id},#{name},#{age},#{sex},#{pwd})
UPDATE person SET name=#{name},age=#{age} WHERE id=1
Person.java
package com.bwf.entity;
public class Person {
private int id;
private String name;
private int age;
private String sex;
private String pwd;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
创建Web工程,使用eclipse ee创建maven web工程 1.右键项目,选择Project Facets,点击Convert to faceted from 2.更改Dynamic Web Module的Version为2.5.(3.0为Java7的,Tomcat6不支持). 如果提示错误,可能需要在Java Compiler设置Compiler compl
最近一直在看python的document,打算在基础方面重点看一下python的keyword、Build-in Function、Build-in Constants、Build-in Types、Build-in Exception这四个方面,其实在看的时候发现整个《The Python Standard Library》章节都是很不错的,其中描述了很多不错的主题。先把Build-in Fu
学习函数式编程
package base;
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
// Integer a = 4;
// Double aa = (double)a / 100000;
// Decimal
Java中的泛型的使用:1.普通的泛型使用
在使用类的时候后面的<>中的类型就是我们确定的类型。
public class MyClass1<T> {//此处定义的泛型是T
private T var;
public T getVar() {
return var;
}
public void setVa